Don212
Don212

Reputation: 55

I need to add an element to an xml file using the DOM in Java

I've a problem adding elements to my xml file using the DOM in my Java

I know there is a lot of solutions to similar questions, but I haven't been successful in getting them to work for me, any help would be appreciated. thanks guys and girls!

My current method for adding a new element

public static void createEntryXmlFile () {
  try{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(FILE)); 


        NodeList nodes = document.getElementsByTagName("CATALOG");


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


        // title elements
        Element title = document.createElement("TITLE");
        title.appendChild(document.createTextNode("TITLE")); 
        cd.appendChild(title);

        // artist elements
        Element artist = document.createElement("ARTIST");
        artist.appendChild(document.createTextNode("ARTIST")); 
        cd.appendChild(artist);

        // company elements
        Element company  = document.createElement("COMPANY");
        company.appendChild(document.createTextNode("COMPANY")); 
        cd.appendChild(company);

        // country elements
        Element country = document.createElement("COUNTRY");
        country.appendChild(document.createTextNode("COUNTRY")); 
        cd.appendChild(country);

        // price elements
        Element price = document.createElement("PRICE");
        price.appendChild(document.createTextNode("PRICE")); 
        cd.appendChild(price);

        // year elements
        Element year = document.createElement("YEAR");
        year.appendChild(document.createTextNode("YEAR")); 
        cd.appendChild(year);

        Node n = (Element) cd;
        nodes.item(0).appendChild(cd);


  } catch (ParserConfigurationException | SAXException | IOException e) {
        System.out.println(e);
    }
}

Copy of the XML file

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

Upvotes: 0

Views: 107

Answers (2)

wypieprz
wypieprz

Reputation: 8219

Add the following code after your DOM manipulation statements so that changes are written into the FILE:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(document), new StreamResult(new File(FILE)));
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
} catch(TransformerException e) {
    e.printStackTrace();
}

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122364

The code you've shown will parse an existing XML file into a DOM tree in memory and then make changes to that tree, but modifying the tree in memory won't affect the file on disk, you need to explicitly write it back out yourself.

The two most common ways to do that are a no-op Transformer from a DOMSource to a StreamResult, or by using the DOM "load and store" API by casting document.getImplementation() to a DOMImplementationLS. Since the Transformer approach uses the XSLT infrastructure it may not play nicely with a non-namespace-aware DOM tree such as you're using (DocumentBuilderFactory is not namespace aware by default, you have to explicitly configure it so before you call newDocumentBuilder) but the LS approach will definitely be OK as that's entirely part of the DOM library, it doesn't involve the transformation layer.

Upvotes: 2

Related Questions