This is again a "getting started tutorial" of how to start using XML parsing concepts in java. For further details of using XML parsing please refer tutorials at : http://docs.oracle.com/javase/tutorial/jaxp/sax/index.html.
The program presented here is a simple demonstration of how to parse an XML document using SAX-XML parser for java. For demonstration purpose a sample .xml file is also included.
temp.xml:
<?xml version="1.0"?>
<company>
<staff department="information security">
<firstname>FIRST NAME-1</firstname>
<lastname>LAST NAME-1</lastname>
<nickname>STUDENT-1</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>FIRST NAME-2</firstname>
<lastname>LAST NAME-2</lastname>
<nickname>STUDENT-2</nickname>
<salary>200000</salary>
</staff>
</company>
Temp.java :
import java.util.Hashtable;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Temp extends DefaultHandler{
private Hashtable tags;
String tempVal=null;
public static void main(String a[])throws Exception
{
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser=spf.newSAXParser();
XMLReader reader=saxParser.getXMLReader();
reader.setContentHandler(new Temp());
reader.parse("temp.xml");
}
public void startDocument()throws SAXException
{
tags=new Hashtable();
System.out.println("------startDocument()--------");
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
{
System.out.println("------startElement() -------");
System.out.println("namespaceURI="+namespaceURI);
System.out.println("localname="+localName);
System.out.println("qName="+qName);
int attributes=atts.getLength();
//Iterator iterator=atts.
for(int i=0; i<attributes; i++)
{
System.out.println("attribute name="+atts.getLocalName(i));
System.out.println("attribute value="+atts.getValue(i));
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
System.out.println("characters()/ Tag content value="+tempVal);
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("---------endELement()-------");
System.out.println("uri="+uri);
System.out.println("localName="+localName);
System.out.println("qName="+qName);
}
}
The program presented here is a simple demonstration of how to parse an XML document using SAX-XML parser for java. For demonstration purpose a sample .xml file is also included.
temp.xml:
<?xml version="1.0"?>
<company>
<staff department="information security">
<firstname>FIRST NAME-1</firstname>
<lastname>LAST NAME-1</lastname>
<nickname>STUDENT-1</nickname>
<salary>100000</salary>
</staff>
<staff>
<firstname>FIRST NAME-2</firstname>
<lastname>LAST NAME-2</lastname>
<nickname>STUDENT-2</nickname>
<salary>200000</salary>
</staff>
</company>
Temp.java :
import java.util.Hashtable;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Temp extends DefaultHandler{
private Hashtable tags;
String tempVal=null;
public static void main(String a[])throws Exception
{
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser saxParser=spf.newSAXParser();
XMLReader reader=saxParser.getXMLReader();
reader.setContentHandler(new Temp());
reader.parse("temp.xml");
}
public void startDocument()throws SAXException
{
tags=new Hashtable();
System.out.println("------startDocument()--------");
}
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
{
System.out.println("------startElement() -------");
System.out.println("namespaceURI="+namespaceURI);
System.out.println("localname="+localName);
System.out.println("qName="+qName);
int attributes=atts.getLength();
//Iterator iterator=atts.
for(int i=0; i<attributes; i++)
{
System.out.println("attribute name="+atts.getLocalName(i));
System.out.println("attribute value="+atts.getValue(i));
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
tempVal = new String(ch,start,length);
System.out.println("characters()/ Tag content value="+tempVal);
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("---------endELement()-------");
System.out.println("uri="+uri);
System.out.println("localName="+localName);
System.out.println("qName="+qName);
}
}
Comments
Post a Comment