使用Java DOM将新元素添加到xml文件

时间:2014-03-14 19:12:49

标签: java xml dom

我一直在使用简单的CD目录XML文件:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>

使用此方法,我想创建一个新的CD元素:

private static void addCd(File xml) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(xml);

        Element cd = document.createElement("CD");

        document.appendChild(cd);
        for (int i = 1; i <= 3; i++) {
            Element title = document.createElement("TITLE");
            Element artist = document.createElement("ARTIST");
            Element country = document.createElement("COUNTRY");
            Element company = document.createElement("COMPANY");
            Element price = document.createElement("PRICE");
            Element year = document.createElement("YEAR");


            title.appendChild(document.createTextNode("mike "));
            artist.appendChild(document.createTextNode("oconnor "));
            country.appendChild(document.createTextNode("ie "));
            company.appendChild(document.createTextNode("dell "));
            price.appendChild(document.createTextNode("14 "));
            year.appendChild(document.createTextNode("2014 "));

            cd.appendChild(title);
            cd.appendChild(artist);
            cd.appendChild(country);
            cd.appendChild(company);
            cd.appendChild(price);
            cd.appendChild(year);
        }

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        StreamResult streamResult = new StreamResult(new File("cdCatalog.xml"));
        transformer.transform(domSource, streamResult);

        DOMSource source = new DOMSource(document);

    }

我可以创建一个新的CD元素,但新元素出现在我的根(<CATALOG>)元素之外:

<?xml version="1.0" encoding="utf-8" standalone="no"?><CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG><CD><TITLE>mike </TITLE><ARTIST>oconnor </ARTIST><COUNTRY>ie </COUNTRY><COMPANY>dell </COMPANY><PRICE>14 </PRICE><YEAR>2014 </YEAR><TITLE>mike </TITLE><ARTIST>oconnor </ARTIST><COUNTRY>ie </COUNTRY><COMPANY>dell </COMPANY><PRICE>14 </PRICE><YEAR>2014 </YEAR><TITLE>mike </TITLE><ARTIST>oconnor </ARTIST><COUNTRY>ie </COUNTRY><COMPANY>dell </COMPANY><PRICE>14 </PRICE><YEAR>2014 </YEAR></CD>

我认为我添加了错误的元素,但不知道如何附加到CATALOG

2 个答案:

答案 0 :(得分:3)

问题是,你是逐个文件追加的,你需要通过根元素附加CD

试试这样:

...
Document document = db.parse(xml);
Element root = document.getDocumentElement();
Element cd = document.createElement("CD");
root.appendChild(cd);
.....
......
....

答案 1 :(得分:1)

您应该使用getElementsByTagName获取第一个catalog标记并将cd对象附加到它,而不是像您正在做的那样附加到文档。