Lab11 XML Parsers


1.    Create  a well-formed XML shown below

data.xml:

<?xml version="1.0" encoding="UTF-8"?>

<school year="2011">
 <class>
  <students>
   <student>Mary</student>
   <student>Susan</student>
  </students>
  <teacher>Robinson Thatcher</teacher>
 </class>
 
 <class>
  <students>
   <student>Jack</student>
   <student>Harry</student>
   <student>Rebecca</student>
  </students>
  <teacher>Larry Edison</teacher>
 </class>
</school>
1.      
        Use SAX to parse the XML data , showing the output display as below:

s           saxParser.parse( "data.xml", new newHandler() );

Extracted Data:



Methods in SAX parser:


  • startDocument()
  • endDocument()
  • startElement()
  • endElement()
  • characters()
Fill in the blanks:
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
public class Parser_SAX {
    public static void main(String argv[]) {
 try {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser  saxParser = factory.newSAXParser();
  saxParser.parse( "data.xml", new newHandler() );
 } catch (Exception e) {
  e.printStackTrace();
 }
    }
}

class newHandler extends DefaultHandler {
    boolean bStudent = false; boolean bTeacher = false;

    public void startDocument() {
 System.out.println( "Classes of students and teachers:" );
    }

    public void startElement(String uri, String localName,String qName, Attributes attributes) {
 if (qName.equalsIgnoreCase("student")) bStudent = true;
 if (qName.equalsIgnoreCase("teacher")) bTeacher = true;
    }

    public void endElement(String uri, String localName, String qName) {
    }

    public void characters(char ch[], int start, int length) {
 if (bStudent) {
  System.out.println( "Student : " + (new String(ch, start, length)).trim() );
  bStudent = false;
 }
 if (bTeacher) {
  System.out.println( "Teacher : " + (new String(ch, start, length)).trim() ); 
  System.out.println("------------------------------------------");
  bTeacher = false;
 }
    }
};


Right clicked on Parser_SAX.java > Run as Java Application




Try it with DOM Parser

Parser_DOM.java:


package mdad.lab11;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class Parser_DOM {
 static public void exploreNode(Node node) {
 if( node.getNodeType()==Node.ELEMENT_NODE ){
 if( node.getNodeName().equals("student") ){
 if ( node.getFirstChild().getNodeType()==Node.TEXT_NODE )
 System.out.println( "Student: " +
node.getFirstChild().getNodeValue()
);
 }
 else if( node.getNodeName().equals("teacher") ){
 if ( node.getFirstChild().getNodeType()==Node.TEXT_NODE ){
 System.out.println( "Teacher: " +
node.getFirstChild().getNodeValue()
);
 System.out.println("------------------------------------------");
 }
 }
 NodeList children = node.getChildNodes();
 for( int i=0; i<children.getLength(); i++ )
 exploreNode( children.item(i) );
 }
 }
 public static void main( String args[] ) {
 Document doc = null;
 try{
 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 DocumentBuilder db = dbf.newDocumentBuilder();
 doc = db.parse("data.xml");
 Element root = doc.getDocumentElement();
 System.out.println( "Classes of students and teachers: \n" );
 ______________________;
 } catch (Exception e){
 System.out.print( "Problem parsing file:" + e.getMessage() );
 }
 } //close for main()
} //close for class 
exploreNode( root );