Google

Jul 23, 2014

Java and XML tutorial with DOM Parser

Q. What is a DOM parser, and when will you use it?
A. The Java DOM API for XML parsing is intended for working with small to medium XML documents as an object graph in memory. The DOM parser traverses the XML file and creates the corresponding DOM objects linked together in a tree structure. Once the parser is done, you get this DOM object structure back from it. Then you can traverse the DOM structure back and forth, and also insert new nodes. If you have large XML files, favor SAX parser if only read only or a StAX parser if you want to read and write.



Step 1: Sample XML to read and insert new element into.

<Employee>
   <name type="first">Peter</name>
   <age>25</age>
</Employee>

Step 2: The Java DOM code to traverse and write to above XML.

package com.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMProcessing {

 public static void main(String[] args) {
  DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = null;
  try {
   builder = builderFactory.newDocumentBuilder();
   String xml = "<Employee><name type=
      \"first\">Peter</name><age>25</age></Employee>";
   Document document = builder.parse(new ByteArrayInputStream(xml.getBytes()));

   // read root node
   Element rootElement = document.getDocumentElement();
   System.out.println(rootElement.getNodeName());

   // read child nodes
   NodeList childNodes = rootElement.getChildNodes();
   System.out.println("Before adding a new node ----------------");
   print(childNodes);

   //add a new node
   if (rootElement.getNodeName().equalsIgnoreCase("Employee")) {
    Node nodeNew = document.createElement("salary");
    nodeNew.setTextContent("35000.00");
    rootElement.appendChild(nodeNew);
   }
   
   System.out.println("After adding a new node ----------------");
   print(childNodes);

  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private static void print(NodeList childNodes) {
  for (int i = 0; i < childNodes.getLength(); i++) {
   Node node = childNodes.item(i);
   System.out.println(node.getNodeName() + "=" + node.getTextContent());
   if (node instanceof Element && node.getNodeName().equalsIgnoreCase("name")) {
    // a child element to process
    Element child = (Element) node;
    String attribute = child.getAttribute("type");
    System.out.println("type=" + attribute);
   }
  }
 }
}


Output:

Employee
Before adding a new node ----------------
name=Peter
type=first
age=25
After adding a new node ----------------
name=Peter
type=first
age=25
salary=35000.00


As you can see, the "salary" is the new node that has been added.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home