简体   繁体   English

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

[英]How to append new nodes to an existing xml file

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. 但每次Im只能重写或损坏xml文件。 ps Im getting all my bookName's and so on from the input ps我从输入中获取了我所有的bookName,依此类推

I can just suggest to use the JDom library. 我可以建议使用JDom库。 Its very easy to use and implement, always worked for me. 它非常易于使用和实施,一直为我工作。

JDOM is, quite simply, a Java representation of an XML document. 简而言之,JDOM是XML文档的Java表示形式。 JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. JDOM提供了一种表示该文档的方式,以方便,高效地进行读取,操作和编写。 It has a straightforward API, is a lightweight and fast, and is optimized for the Java programmer. 它具有简单易用的API,轻巧且快速,并且针对Java程序员进行了优化。 It's an alternative to DOM and SAX, although it integrates well with both DOM and SAX. 尽管它可以与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/ http://www.mkyong.com/java/how-to-modify-xml-file-in-java-jdom/

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM