• Entries (RSS)
  • Comments (RSS)

Parsing XML String using DOM

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

Tagged Under : , ,

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

        <city>New Jersey</city>

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());


Read More


10 comments posted onParsing XML String using DOM

  1. 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. 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.


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

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

  5. Tnx

  6. Hi Sir,

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

    examlpe :


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

    Thank You

  7. 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.

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

  9. Thank you so much. A simple example is all I needed to get going. I think some guys just like to overcomplicate stuff. Makes them feel smarter I guess. Thanks a lot

  10. For its not working in android 4.0 version other lower versions working fine.

    Rajesh Rajaram

Post a Comment