【问题标题】:How to append new nodes to an existing xml file如何将新节点附加到现有的 xml 文件
【发布时间】:2015-12-14 13:17:12
【问题描述】:

您好,我是使用 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 我从输入中得到我所有的书名等等

【问题讨论】:

    标签: java xml parsing dom


    【解决方案1】:

    我只能建议使用 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/

    【讨论】:

    【解决方案2】:

    问题已解决。我的解决方案是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2018-06-30
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多