Friday, February 15, 2013

How do I append a node to an existing XML file in java

public static void addALLToXML(Collection<Server> svr) throws IOException,
          ParserConfigurationException, TransformerException
      {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory
            .newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        // Root Element
        Element rootElement = document.createElement("Servers");
        document.appendChild(rootElement);

        for (Server i : svr)
        {
          // server elements
          Element server = document.createElement("server");
          rootElement.appendChild(server);

          Element name = document.createElement("name");
          name.appendChild(document.createTextNode(i.getName()));
          server.appendChild(name);

          Element port = document.createElement("port");
          port.appendChild(document.createTextNode(Integer.toString(i.getPort())));
          server.appendChild(port);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);

        StreamResult result = new StreamResult(
            "/home/user/server.xml");
        transformer.transform(source, result);

      }
 
 
//This function below is what I need help with...
 
public static void addNodeToXML(String nameIn, String portIn)
      throws ParserConfigurationException, SAXException, IOException
  {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory
        .newDocumentBuilder();

    /* parse existing file to DOM */
    Document document = documentBuilder
        .parse(new File(
            "/home/user/server.xml"));

    // Root Element
    Element rootElement = document.createElement("Servers");
    document.appendChild(rootElement);

    // server elements
    Element server = document.createElement("server");
    rootElement.appendChild(server);

    Element name = document.createElement("name");
    name.appendChild(document.createTextNode(nameIn));
    server.appendChild(name);

    Element port = document.createElement("port");
    port.appendChild(document.createTextNode(portIn));
    server.appendChild(port);

  }
 
 
Desired


Original:
something port
New:
something port something port port
 
You can parse the existing XML file into DOM and append new elements to 
the DOM. Very similar to what you did with creating brand new XML. I am 
assuming you do not have to worry about duplicate server. If you do have
 to worry about that, you will have to go through the  elements in the 
DOM to check for duplicates.
 
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

/* parse existing file to DOM */
Document document = documentBuilder.parse(new File("exisgint/xml/file"));

Element root = document.getDocumentElement();

for (Server newServer : Collection<Server> bunchOfNewServers){
  Element server = Document.createElement("server");
  /* create and setup the server node...*/

 root.appendChild(server);
}

/* use whatever method to output DOM to XML (for example, using transformer like you did).*/
share|edit|flag

To append a new data element,just do this...
Document doc = docBuilder.parse(is);        
Node root=doc.getFirstChild();
Element newserver=doc.createElement("new_server");
root.appendChild(newserver);
easy.... 'is' is an InputStream object. rest is similar to your code....tried it just now...
share|edit|flag

 
 
not much new compared to the accepted answer, is there ;-) – kleopatra Oct 27 '12 at 10:31
If you need to insert node/element in some specific place , you can to do next steps
  1. Divide original xml into two parts
  2. Append your new node/element as child to first first(the first part should ended with element after wich you wanna add your element )
  3. Append second part to the new document.
It is simple algorithm but should works...
share|edit|flag

Your Answer

 

Not the answer you're looking for? Browse other questions tagged or ask your own question.

site design / 
  

No comments: