Anda di halaman 1dari 3

import

import
import
import
import

javax.xml.parsers.SAXParser;
javax.xml.parsers.SAXParserFactory;
org.xml.sax.Attributes;
org.xml.sax.SAXException;
org.xml.sax.helpers.DefaultHandler;

public class ReadXml extends DefaultHandler{


public void getXml(){
try {
// obtain and configure a SAX based parser
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
// obtain object for SAX parser
SAXParser saxParser = saxParserFactory.newSAXParser();
// default handler for SAX handler class
// all three methods are written in handler's body
DefaultHandler defaultHandler = new DefaultHandler(){
String
String
String
String

firstNameTag="close";
lastNameTag="close";
emailTag="close";
phoneTag="close";

// this method is called every time the parser gets an open tag '<'
// identifies which tag is being open at time by assigning an open flag
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("FIRSTNAME")) {
firstNameTag = "open";
}
if (qName.equalsIgnoreCase("LASTNAME")) {
lastNameTag = "open";
}
if (qName.equalsIgnoreCase("EMAIL")) {
emailTag = "open";
}
if (qName.equalsIgnoreCase("PHONE")) {
phoneTag = "open";
}
}
// prints data stored in between '<' and '>' tags
public void characters(char ch[], int start, int length)
throws SAXException {
if (firstNameTag.equals("open")) {
System.out.println("First Name : " + new String(ch, start, length));
}
if (lastNameTag.equals("open")) {
System.out.println("Last Name : " + new String(ch, start, length));
}
if (emailTag.equals("open")) {
System.out.println("Email : " + new String(ch, start, length));
}
if (phoneTag.equals("open")) {
System.out.println("Phone : " + new String(ch, start, length));
}

}
// calls by the parser whenever '>' end tag is found in xml
// makes tags flag to 'close'
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("firstName")) {
firstNameTag = "close";
}
if (qName.equalsIgnoreCase("lastName")) {
lastNameTag = "close";
}
if (qName.equalsIgnoreCase("email")) {
emailTag = "close";
}
if (qName.equalsIgnoreCase("phone")) {
phoneTag = "close";
}
}
};
// parse the XML specified in the given path and uses supplied
// handler to parse the document
// this calls startElement(), endElement() and character() methods
// accordingly
saxParser.parse("xmlToRead/student.xml", defaultHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
**********************
Implementation Class code
public class SaxParserImplementation {
public static void main(String args[]){
ReadXml readXml = new ReadXml();
readXml.getXml();
}
}

Methods:
ContentHandler Interface
This interface specifies the callback methods that the SAX parser uses to notify
an application program of the components of the XML document that it has seen.
void startDocument() - Called at the beginning of a document.
void endDocument() - Called at the end of a document.
void startElement(String uri, String localName, String qName, Attributes att

s) - Called at the beginning of an element.


void endElement(String uri, String localName,String qName) - Called at the e
nd of an element.
void characters(char[] ch, int start, int length) - Called when character da
ta is encountered.
void ignorableWhitespace( char[] ch, int start, int length) - Called when a
DTD is present and ignorable whitespace is encountered.
void processingInstruction(String target, String data) - Called when a proce
ssing instruction is recognized.
void setDocumentLocator(Locator locator)) - Provides a Locator that can be u
sed to identify positions in the document.
void skippedEntity(String name) - Called when an unresolved entity is encoun
tered.
void startPrefixMapping(String prefix, String uri) - Called when a new names
pace mapping is defined.
void endPrefixMapping(String prefix) - Called when a namespace definition en
ds its scope.
Attributes Interface
This interface specifies methods for processing the attributes connected to an e
lement.
int getLength() - Returns number of attributes.
String getQName(int index)
String getValue(int index)
String getValue(String qname)

Anda mungkin juga menyukai