Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add new element to xml file with Java DOM

Tags:

java

dom

xml

I have been working with a simple cd catalog XML file:

<?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>

With this method I want to create a new CD element:

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

    }

I can create a new CD element, but the new element appears outside my root (<CATALOG>) element:

<?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>

I think I appended the wrong element, but have no idea how to append to CATALOG.

like image 723
sir_K Avatar asked Oct 26 '25 21:10

sir_K


2 Answers

The problem is, you were append element by document, YOU needs to append CD by root element

Try like this:

...
Document document = db.parse(xml);
Element root = document.getDocumentElement();
Element cd = document.createElement("CD");
root.appendChild(cd);
.....
......
....
like image 98
Suzon Avatar answered Oct 28 '25 12:10

Suzon


You should use the getElementsByTagName to get the first catalog tag and append your cd object to it, not to the document like you're doing.

like image 26
Augusto Avatar answered Oct 28 '25 12:10

Augusto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!