Featured Posts

Integrating GlassFish Application server with Apache... A typical production topology for GlassFish will be a front ending GlassFish with Apache for serving the static files. To integrate GlassFish Application Server with Apache web server follow the below...

Readmore

Some Java, JEE and WebSphere stuffs Rss

Parsing XML String using DOM

Posted by Albin Joseph | Posted in Java | Posted on 09-10-2007

8

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
6,156 views

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());
			}
	        }
        }
}
  • Share/Bookmark

Read More

Comments (8)

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

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

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

Yes this code just plain doesn’t work! no such object as Document!

instead of “org.xml.sax.Document” try using “org.w3c.dom.Document” ;-)

Tnx

Hi Sir,

Can you please send me the code which parses XML String in JSP.

examlpe :

data=”
Sachin
101

This XML String comes from Servlet to JSP. In JSP “data” XML String must be parsed to display individual elements.

Thank You

Create a class in the same way I have mentioned in this post and call that class from within your JSP, that would be the best way to parse XML from your JSP. If you are facing some problems in that, please let me know. I might be able to help you.

Write a comment