• 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

Post a Comment