您好我是使用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我从输入
获取所有bookName's等等答案 0 :(得分:0)
我可以建议使用JDom库。它非常易于使用和实施,总是为我工作。
JDOM非常简单,是XML文档的Java表示。 JDOM提供了一种表示该文档的方法,以便于阅读,操作和编写。它有一个简单的API,轻量级,快速,并针对Java程序员进行了优化。它是DOM和SAX的替代品,尽管它与DOM和SAX都很好地集成。
mykong.com上有一些很好的教程解释如何阅读,修改和创建xml文件:
http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/
答案 1 :(得分:0)
问题解决了。我的解决方案是:
File fXmlFile = new File("Test/Books.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
// doc.getDocumentElement().normalize();
Element nList = doc.getDocumentElement();
System.out.println("-----------------------");
//root a new book
Element newBook = doc.createElement("Book");
//setting ganre for a book
Attr att = doc.createAttribute("ganre");
att.setValue(ganre);
newBook.setAttributeNode(att);
//book id
Element bookId = doc.createElement("bookId");
bookId.appendChild(doc.createTextNode(randomString(4)));
newBook.appendChild(bookId);
//bookname element
Element bookname = doc.createElement("bookName");
bookname.appendChild(doc.createTextNode(name));
newBook.appendChild(bookname);
//book author
Element bookAuthor = doc.createElement("bookAuthor");
bookAuthor.appendChild(doc.createTextNode(author));
newBook.appendChild(bookAuthor);
//book year
Element bookYear = doc.createElement("bookYear");
bookYear.appendChild(doc.createTextNode(String.valueOf(year)));
newBook.appendChild(bookYear);
//book available
Element bookAvail = doc.createElement("bookAvailable");
bookAvail.appendChild(doc.createTextNode(String.valueOf(free)));
newBook.appendChild(bookAvail);
nList.appendChild(newBook);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new File("Test/Books.xml"));
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
System.out.println("DONE");