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.
Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts
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.
XML Parsers
XML Parsing in Java:
XML parser is a software module to read documents and a means to provide access to their content. XML parser generates a structured tree to return the results to the browser. An XML parser is similar to a processor that determines the structure and properties of the data. An XML parser can read a XML document to create an output to generate a display form.
Types of XML Parser:
1. SAX Parser
2. DOM Parser
SAX Parser:
Simple API for XML (SAX) was developed by the members of a public mailing list (XML-DEV).It gives an event based approach to XML parsing. It means that instead of going from node to node, it goes from event to event. SAX is an event driven interface. Events include XML tag, detecting errors etc,
DOM Parser:
DOM Parser stands for Document Object Model. This read the entire XML Files and puts in the memory. This makes traversal easy and efficient. This should only be used for small XML Files. This operates faster, but takes up more memory.
In the next post, we will see an example of DOM Parser in Java.
XML parser is a software module to read documents and a means to provide access to their content. XML parser generates a structured tree to return the results to the browser. An XML parser is similar to a processor that determines the structure and properties of the data. An XML parser can read a XML document to create an output to generate a display form.
Types of XML Parser:
1. SAX Parser
2. DOM Parser
SAX Parser:
Simple API for XML (SAX) was developed by the members of a public mailing list (XML-DEV).It gives an event based approach to XML parsing. It means that instead of going from node to node, it goes from event to event. SAX is an event driven interface. Events include XML tag, detecting errors etc,
DOM Parser:
DOM Parser stands for Document Object Model. This read the entire XML Files and puts in the memory. This makes traversal easy and efficient. This should only be used for small XML Files. This operates faster, but takes up more memory.
In the next post, we will see an example of DOM Parser in Java.
Subscribe to:
Posts (Atom)