Thursday, May 29, 2014

DOM (Document Object Model) usage with Java [JAXP][XML][DOM][Java][Note]

I assume that you already know what DOM is. If you don't, have a quick look at this short tutorial here.
To use the DOM with Java, you first need the library (JAXP = Java API for XML Processing).

You can download this from here.

Add this library to your CLASSPATH environment variable. I'll just be explaining the basic stuff here to get you going.

Let us consider this file as our test XML file:

<?xml version="1.0"?>
<Contact>
  <Name>Peter Parker</Name>
  <Phone>333-222-1234</Phone>
  <E-Mail>Peter@Parker.com</E-Mail>
  <Comment><!-- This is a comment --></Comment>
</Contact>

As you can see, according to DOM, the tree will be like this:

|HTML
  • -|HEAD
  • -|BODY
    • -|CONTACT
      • -|#text:
      • -|NAME
        • -|#textPeter Parker
      • -|#text:
      • -|PHONE
        • -|#text333-222-1234
      • -|#text:
      • -|E-MAIL
        • -|#textPeter@Parker.com
      • -|#text:
      • -|COMMENT
        • -|#commentThis is a comment
      • -|#text:

This Online DOM Viewer will be helpful for your own XML files : Online DOM Viewer

So I'll be extracting the Information from the XML file to the standard output stream. Most of the code has been explained in the comments.

import java.io.File;
import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class XMLJava {
    public static void print(Node node){
            NodeList nl = node.getChildNodes();
            for(int i=0;i<nl.getLength();++i){
                Node nod = nl.item(i);
                /*
                  Note that the comparision is important here
                  else the parser will include the #text nodes also which 
                  do not have any child so that nod.getFirstChild()
                  will return NULL and you'll 
                  get a nice NullPointerException
                */
                if(nod.getNodeType()==Node.ELEMENT_NODE)
                /*
                  Notice that the Node for eg. Name is of type 
                  ELEMENT and if you call get value on a node
                  of type element, you'll get null always (according
                  to the DOM (Document Object Model). The actual text
                  element is it's child. 
                  So you need to nod.getFirstChild() to get the node
                  with the text in it and then call 
                  getNodeValue() to get the text
                */
                System.out.println(nl.item(i).getNodeName()+
                               ": "+nod.getFirstChild().getNodeValue());
             }
    }
    
    public static void main(String[] args) throws
                        SAXException, IOException, ParserConfigurationException
        {
        //This string will store the location of the
        //xml file to be processed by the parser
        String xmlDocPath = "e:\\data.xml";
        
        //We need to instantiate the factory first
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //A DocumentBuilder Object to Parse XML Files and return the DOC model
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(xmlDocPath));
        
        //Let's print the Root Name First (The result of getNodeValue() 
        //will be NULL here as it is of the ELEMENT type
        System.out.println("Root : "+doc.getDocumentElement().getNodeName());
        //Then the children
        print(doc.getDocumentElement());
       }
}

This Code will get you this output:

Root : Contact
Name: Peter Parker
Phone: 333-222-1234
E-Mail: Peter@Parker.com
Comment:  This is a comment 

Which is what we wanted.
If you want to get some more information on DOM (Java), visit here.

[[[How to create a DOM tree dynamically and then use the information in your code]]]

We'll be making a DOM tree from scratch by adding element and text nodes to it. So that the required XML structure should be:

<Contact>
  <Name>Peter Parker</Name>
  <Address>123 SomeStreet, Some City</Address>
  <PhoneNumber>123-222-1234</PhoneNumber>
  <E-Mail>Peter@Parker.com</E-Mail>
</Contact>


It's DOM will be:

  • HTML
    • HEAD
    • BODY
      • CONTACT
        • #text:
        • NAME
          • #textPeter Parker
        • #text:
        • ADDRESS
          • #text123 SomeStreet, Some City
        • #text:
        • PHONENUMBER
          • #text123-222-1234
        • #text:
        • E-MAIL
          • #textPeter@Parker.com
        • #text:
      • #text:

Here's the code. It must be easy to understand by itself. I've added some comments wherever necessary.

import java.io.File;
import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class XMLJava {
    public static void main(String[] args) throws
            SAXException, IOException, ParserConfigurationException{
        //This string will store the location of the
        //xml file to be processed by the parser
        String xmlDocPath = "e:\\data.xml";
        
        //We need to instantiate the factory first
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //A DocumentBuilder Object to Parse XML Files and return the DOC model
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new File(xmlDocPath));
        
        //Create The <Contact> tag element
        Node contact = doc.createElement("Contact");
        
        //Make this the parent of the subelements
        doc.appendChild(contact);
        
        Node Name = doc.createElement("Name");  //<Name> tag 
        Node tName = doc.createTextNode("Peter Parker"); //the text in it
        
        Node Address = doc.createElement("Address"); //<Address> tag
        Node tAddress = doc.createTextNode("123 SomeStreet, Some City");
        
        Node PhoneNumber = doc.createElement("PhoneNumber");
        Node tPhone = doc.createTextNode("123-222-1234");
        
        Node EmailID = doc.createElement("EmailID");
        Node tEmail = doc.createTextNode("peter@parker.com");
        
        //Make the structure of the DOM tree
        contact.appendChild(Name);  
        contact.appendChild(Address);
        contact.appendChild(PhoneNumber);
        contact.appendChild(EmailID);
        
        Name.appendChild(tName);
        Address.appendChild(tAddress);
        PhoneNumber.appendChild(tPhone);
        EmailID.appendChild(tEmail);
        
        //Display the results
System.out.println(doc.getElementsByTagName("Name").item(0).getNodeName()        +": "+doc.getElementsByTagName("Name").item(0).getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("Address").item(0).getNodeName()+": "+doc.getElementsByTagName("Address").item(0).getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("PhoneNumber").item(0).getNodeName()+": "+doc.getElementsByTagName("PhoneNumber").item(0).getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("EmailID").item(0).getNodeName()+": "+doc.getElementsByTagName("EmailID").item(0).getFirstChild().getNodeValue());   
    }
}

It gives the output:

Name: Peter Parker
Address: 123 SomeStreet, Some City
PhoneNumber: 123-222-1234
EmailID: peter@parker.com

As expected.

No comments:

Post a Comment