Add new element to xml file with Java DOM

10,589

Solution 1

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

Solution 2

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.

Share:
10,589
sir_K
Author by

sir_K

Updated on June 05, 2022

Comments

  • sir_K
    sir_K almost 2 years

    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.