Hello Im new in useing xml and now trying to append new nodes for existing xml file.
你好我是新的使用xml,现在尝试为现有的xml文件追加新的节点。
This is my code to write a 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!");
this is how I trying to append new nodes
这就是我尝试追加新节点的方法
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]
what i want to do is rewrite file like this:
我想要做的是重写这样的文件:
<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>
but every time Im can only rewrite or damage the xml file. ps Im getting all my bookName's and so on from the input
但每次我只能重写或损坏xml文件。 ps我从输入中得到我所有的bookName等等
2 个解决方案
#1
0
I can just suggest to use the JDom library. Its very easy to use and implement, always worked for me.
我可以建议使用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.
简而言之,JDOM是XML文档的Java表示。 JDOM提供了一种表示该文档的方法,以便于阅读,操作和编写。它有一个简单的API,轻量级,快速,并针对Java程序员进行了优化。它是DOM和SAX的替代品,尽管它与DOM和SAX都很好地集成。
There are some good tutorials at mykong.com explaining how to read, modify and create xml-files :
mykong.com上有一些很好的教程,解释了如何阅读,修改和创建xml文件:
http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/
#2
0
problem was solved. My solution is:
问题解决了。我的解决方案是:
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");
#1
0
I can just suggest to use the JDom library. Its very easy to use and implement, always worked for me.
我可以建议使用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.
简而言之,JDOM是XML文档的Java表示。 JDOM提供了一种表示该文档的方法,以便于阅读,操作和编写。它有一个简单的API,轻量级,快速,并针对Java程序员进行了优化。它是DOM和SAX的替代品,尽管它与DOM和SAX都很好地集成。
There are some good tutorials at mykong.com explaining how to read, modify and create xml-files :
mykong.com上有一些很好的教程,解释了如何阅读,修改和创建xml文件:
http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/
#2
0
problem was solved. My solution is:
问题解决了。我的解决方案是:
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");