我試圖從控制檯接收用戶輸入並將其提供給XML文件。 Evrytime用戶移動到下一行我想要輸入它們的字符串並創建一個新元素。 這是我想實現:從用戶輸入創建XML元素
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<note>
<header>
<Tempo Laya="2"/>
</header>
<Notes>
<n1 Bol="Text the user entered"/>
<n2 Bol="Text the user entered over the next iteration"/>
<n3 Bol="Text the user entered over the next iteration"/>
</Notes>
</note>
我認爲這樣做會然而,創建這些元素,最好的辦法;我無法通過此創建獨特的元素名稱。這是我到目前爲止的代碼:
//Create note element
Element notes = doc.createElement("Notes");
rootElement.appendChild(notes);
System.out.println("Input your notes matraa by maatra. To proceed to the next maatra press ENTER. \n To exit enter END");
do{
int noteCount = 1;
System.out.println("Maatra: ");
bol = scanner.nextLine();
}while(scanner.nextLine()!= "END");
有什麼辦法來創建和附加使用上述循環的元素。如果不是我可以怎麼做呢?
你不應該這樣編號你的節點。將它們全部命名爲'Note',並且任何代碼關心它們的順序都可以通過它們在Notes節點中的順序來確定它,或者您可以添加一個「數字」屬性 – Bohemian