gpt4 book ai didi

java - 如何将新节点附加到现有 xml 文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:08 25 4
gpt4 key购买 nike

您好,我是 xml 使用新手,现在尝试为现有 xml 文件附加新节点。

这是我编写 xml 的代码

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBulder = docFactory.newDocumentBuilder();

//root mainElement
Document doc = docBulder.newDocument();
Element rootElement = doc.createElement("Books");
doc.appendChild(rootElement);

//root Book
Element Book = doc.createElement("Book");
rootElement.appendChild(Book);

//setting ganre for a book
Attr att = doc.createAttribute("ganre");
att.setValue(ganre);
Book.setAttributeNode(att);

//book id
Element bookId = doc.createElement("bookId");
bookId.appendChild(doc.createTextNode(randomString(4)));
Book.appendChild(bookId);

//bookname element
Element bookname = doc.createElement("bookName");
bookname.appendChild(doc.createTextNode(name));
Book.appendChild(bookname);

//book author
Element bookAuthor = doc.createElement("bookAuthor");
bookAuthor.appendChild(doc.createTextNode(author));
Book.appendChild(bookAuthor);

//book year
Element bookYear = doc.createElement("bookYear");
bookYear.appendChild(doc.createTextNode(String.valueOf(year)));
Book.appendChild(bookYear);

//book available
Element bookAvail = doc.createElement("bookAvailable");
bookAvail.appendChild(doc.createTextNode(String.valueOf(free)));
Book.appendChild(bookAvail);

//write in a XMLFile
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("Test/Books.xml"));

transformer.transform(source, result);

System.out.println("File saved!");

这就是我尝试附加新节点的方式

     File fXmlFile = new File("Test/Books.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

Node books = doc.getFirstChild();


Element newBook = doc.createElement("Book");

[here the troubles comes]

我想做的是像这样重写文件:

    <Books>
<Book ganre="fantasy">
<bookId>7111</bookId>
<bookName>Tron</bookName>
<bookAuthor>Brawm</bookAuthor>
<bookYear>15</bookYear>
<bookAvailable>true</bookAvailable>
</Book>

<Book ganre="action">
<bookId>312</bookId>
<bookName>Corn</bookName>
<bookAuthor>Down</bookAuthor>
<bookYear>23</bookYear>
<bookAvailable>false</bookAvailable>
</Book>
</Books>

但每次我只能重写或损坏xml文件。ps 我从输入中获取我所有的书名等

最佳答案

我只能建议使用 JDom 库。它非常易于使用和实现,一直对我有用。

JDOM is, quite simply, a Java representation of an XML document. JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. It has a straightforward API, is a lightweight and fast, and is optimized for the Java programmer. It’s an alternative to DOM and SAX, although it integrates well with both DOM and SAX.

mykong.com 有一些很好的教程,解释如何读取、修改和创建 xml 文件:

http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/

关于java - 如何将新节点附加到现有 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32634240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com