Sunday, February 11, 2007

Reading simple XML File using DOM Parser

In this example, we will write Java code to read the contents of a simplest XML file and print it.

package com;

import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;


public class XMLLesson {

/*public String getNodeTextValue(NodeList nl)
{
Node node = null;
for(int i = 0 ; i< node =" nl.item(i);" t =" (Text)">

public void parseXML(String fname)
{

DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder bld = null;
Document doc = null;
try {
bld = fact.newDocumentBuilder();
doc = bld.parse(fname);

} catch(Exception e)
{
e.printStackTrace() ;
}

Element parentElement = doc.getDocumentElement();
NodeList nl = parentElement.getElementsByTagName("child1");
int i;
System.out.println("Parent Element : " + parentElement.getTagName() );
for(i = 0;i<nl.getLength();++i) {
System.out.println("i = " + i + " " + nl.item(i).getFirstChild().getNodeValue() );
}


//System.out.println(getNodeTextValue(n));

}

public static void main(String[] args) {
XMLLesson l = new XMLLesson();
l.parseXML("c:\\temp.xml");
}

}

Contents of xml file:

child1data
child1data
child1data


Output is :
Parent Element : parent
i = 0 child1data
i = 1 child1data
i = 2 child1data

Explanation:
This code is written for Java 1.5. This use XML Parser provided by Sun. There are many XML
parsers available, including xerces and xml parser api from IBM.

All the xml file will have a document element. All the tags will be inside that element
only. So first we get the document element. Then get the list of child nodes associate
with it.

In order to get the text content of the tag,
data
"node.getFirstChild().getNodeValue()"
node.getFirstChild() return the value node, and calling getNodeValue returns the value.

you could also use node.getTextContent(), to get the text content of the node.




No comments: