Search code examples
javaxml

How to add multiple child nodes with the same tag to an existing node in an XML document


I have a configuration file stored in XML with a structure like this:

.
<configuration>
.
.
.
<history></history>
.
.
</configuration>
.

I would like to end up with the following:

<configuration>
.
.
<history>
   <history_url>http://www.oldurl1.com</history_url>
   <history_url>http://www.oldurl2.com</history_url>
   <history_url>http://www.oldurl3.com</history_url>
</history>
.
.
</configuration>

I've looked at the Node interface which has the method appendChild(Node newChild), but this states "If the newChild is already in the tree, it is first removed."

What is the easiest way to add multiple <history_url> nodes to the existing <history_node>?

The Node method insertBefore also states "If the newChild is already in the tree, it is first removed". I've thought about creating a NodeList object, but I don't see any way to add a NodeList.


Solution

  • You can use appendChild. The equivalence of a node is going to look at the object equality. Here is example code for adding the multiple history_url child elements.

    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    
    public class NodeMultipleElementAdd {
    
    
      public static void main(String[] args) {
          try {
              DocumentBuilderFactory dbFactory =
              DocumentBuilderFactory.newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.newDocument();
              
              // root element
              Element rootElement = doc.createElement("configuration");
              doc.appendChild(rootElement);
    
              // history element
              Element history = doc.createElement("history");
              rootElement.appendChild(history);
    
    
              // history url element1
              Element historyUrl = doc.createElement("history_url");
              historyUrl.appendChild(doc.createTextNode("http://www.oldurl1.com"));
              history.appendChild(historyUrl);
    
              // history url element2
              Element historyUrl2 = doc.createElement("history_url");
              historyUrl2.appendChild(doc.createTextNode("http://www.oldurl2.com"));
              history.appendChild(historyUrl2);
    
              // history url element3
              Element historyUrl3 = doc.createElement("history_url");
              historyUrl3.appendChild(doc.createTextNode("http://www.oldurl3.com"));
              history.appendChild(historyUrl3);
              
              // write the content into xml file
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
              Transformer transformer = transformerFactory.newTransformer();
              DOMSource source = new DOMSource(doc);
              StreamResult result = new StreamResult(new File("C:\\temp\\history.xml"));
              transformer.transform(source, result);
              
              // Output to console for testing
              StreamResult consoleResult = new StreamResult(System.out);
              transformer.transform(source, consoleResult);
           } catch (Exception e) {
              e.printStackTrace();
           }
        }
    }