如何创建多级 xml 文件
How to create multi level xml file
我无法找到有关在 xml 文件中创建多级标签的信息
例如我想要下一个结构
<UserCards>
<UserCard userCardId="171">
<userName>somename</userName>
<userSurname>somesurname</userSurname>
<userAge>24</userAge>
<userAdress>someadress</userAdress>
<userPhone>223334455</userPhone>
<CurrentBooks>
<booName>someBookName</bookName>
</CurrentBooks>
</UserCard>
</UserCards>
我可以创建一个简单的关卡 xml 但我该如何添加新关卡?
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBulder = docFactory.newDocumentBuilder();
//root mainElement
Document doc = docBulder.newDocument();
Element rootElement = doc.createElement("UserCards");
doc.appendChild(rootElement);
//root Book
Element UserCard = doc.createElement("UserCard");
rootElement.appendChild(UserCard);
...
...
//write in a XMLFile
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("Test/UserCards.xml"));
在我看来就像是你自己回答的....
您可以将元素附加到任何元素,而不仅仅是根。
您通过调用 doc.createElement("name")
创建所有元素
并附加到您选择的父元素:
Elmenet userName = doc.createElement("userName");
Text userNameText = doc.createTextNode("somename");
userName.appendChild(userNameText);
UserCard.appendChild(userName);
试试这个
Element rootElement = doc.createElement("UserCards");
doc.appendChild(rootElement);
//root Book
Element UserCard = doc.createElement("UserCard");
UserCard.setAttribute("userCardId" , "171");
Element userSurname = doc.createElement("userSurname");
UserCard.appendChild(userSurname);
Element userAge = doc.createElement("userAge");
UserCard.appendChild(userAge);
Element userAdress = doc.createElement("userAdress");
UserCard.appendChild(userAdress);
Element userPhone = doc.createElement("userPhone");
UserCard.appendChild(userPhone);
Element CurrentBooks = doc.createElement("CurrentBooks");
Element booKName = doc.createElement("booKName");
CurrentBooks.appendChild(booKName);
UserCard.appendChild(CurrentBooks);
rootElement.appendChild(UserCard);
我无法找到有关在 xml 文件中创建多级标签的信息 例如我想要下一个结构
<UserCards>
<UserCard userCardId="171">
<userName>somename</userName>
<userSurname>somesurname</userSurname>
<userAge>24</userAge>
<userAdress>someadress</userAdress>
<userPhone>223334455</userPhone>
<CurrentBooks>
<booName>someBookName</bookName>
</CurrentBooks>
</UserCard>
</UserCards>
我可以创建一个简单的关卡 xml 但我该如何添加新关卡?
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBulder = docFactory.newDocumentBuilder();
//root mainElement
Document doc = docBulder.newDocument();
Element rootElement = doc.createElement("UserCards");
doc.appendChild(rootElement);
//root Book
Element UserCard = doc.createElement("UserCard");
rootElement.appendChild(UserCard);
...
...
//write in a XMLFile
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("Test/UserCards.xml"));
在我看来就像是你自己回答的....
您可以将元素附加到任何元素,而不仅仅是根。
您通过调用 doc.createElement("name")
创建所有元素
并附加到您选择的父元素:
Elmenet userName = doc.createElement("userName");
Text userNameText = doc.createTextNode("somename");
userName.appendChild(userNameText);
UserCard.appendChild(userName);
试试这个
Element rootElement = doc.createElement("UserCards");
doc.appendChild(rootElement);
//root Book
Element UserCard = doc.createElement("UserCard");
UserCard.setAttribute("userCardId" , "171");
Element userSurname = doc.createElement("userSurname");
UserCard.appendChild(userSurname);
Element userAge = doc.createElement("userAge");
UserCard.appendChild(userAge);
Element userAdress = doc.createElement("userAdress");
UserCard.appendChild(userAdress);
Element userPhone = doc.createElement("userPhone");
UserCard.appendChild(userPhone);
Element CurrentBooks = doc.createElement("CurrentBooks");
Element booKName = doc.createElement("booKName");
CurrentBooks.appendChild(booKName);
UserCard.appendChild(CurrentBooks);
rootElement.appendChild(UserCard);