Parsing XML String using DOM

The first step in parsing an XML String using DOM is to get the org.w3c.dom.Document object from our String object. Here I am going to parse the following XML which is stored in a String variable xmlString

<data>
   <address>
          <name>Tom</name>
          <city>Bangalore</city>
    </address>
   <address>
        <name>Chris</name>
        <city>New Jersey</city>
   </address>
</data>

First lets create a Document object using xmlString object.

    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder db = factory.newDocumentBuilder();
    org.xml.sax.InputSource inStream = new org.xml.sax.InputSource();
 
    inStream.setCharacterStream(new java.io.StringReader(xmlString));
    org.xml.sax.Document doc = db.parse(inStream);

Once we got the Document object we need to get the NodeList from our Document object.

org.w3c.dom.NodeList nodeList = doc.getElementsByTagName("address");

This would return all the address elements available in our XML. Next we need to loop through all the nodes in our NodeList and get the nodes present.

for(int index=0; index < nodeList.getLength(); index++) {
      org.w3c.dom.Node node = nodeList.item(index);
}

Once we got the Node we need to cast it to an org.w3c.dom.Element object if the Node is of type org.w3c.dom.Element

if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
      org.w3c.dom.Element element = (org.w3c.dom.Element) node;
}

Now we need to find out all the names that is coming under the current address.

 
org.w3c.dom.NodeList nameNode = element.getElementsByTagName("name");

Now we need to do all the above exercise again to get the final element from where we can get the value.

for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++) {
    if (nameNode.item(iIndex).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
         org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nameNode.item(iIndex);
         System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
     }
}

So the complete code is

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(inStream);	
 
NodeList nodeList = doc.getElementsByTagName("address");
 
for(int index=0; index < nodeList.getLength(); index++)
{
        Node node = nodeList.item(index);
 
	if (node.getNodeType() == Node.ELEMENT_NODE)
	{
	        Element element = (Element) node;
 
		NodeList nameNode = element.getElementsByTagName("name");
 
		for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
		{
		        if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
			{
				Element nameElement = (Element) nameNode.item(iIndex);
				System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
			}
	        }
        }
}
Did you like this? If so, please
tell a friend
about it, and subscribe to the blog RSS feed.

Share/Save/Bookmark

If you enjoyed this post, make sure you subscribe to my RSS feed!



Related Posts:
  • Recursively delete a directory
  • Sending an e-mail using WebSphere Mail session settings.
  • Sorting an ArrayList of objects.
  • LIKE clause with PreparedStatement
  • Exception stackTrace to a String Variable


  • 3 Responses to “Parsing XML String using DOM”  

    1. 1 Sherlack

      Hi, i’m receiving the following error “Reference is not allowed in prolog” on Document doc = db.parse(inStream); line.

      Anyone have encounter this error before? i need help please

    2. 2 Albin Joseph

      I haven’t faced any problem in parsing the XML using the above method. I feel something might be wrong with your xml. Can you share the xml? so that I can have a look.

      -Albin

    3. 3 Mary

      i’m also having the same problem as above :???:

    Leave a Reply