[JAVA100 example]069, parsing XML with sax

Source: Internet
Author: User

Readxmldemo.java


import java.io.*;


import javax.xml.parsers.*;


import org.xml.sax.*;


import org.xml.sax.helpers.*;


public class Readxmldemo extends DefaultHandler {


public static void main (string[] arguments) {


if (arguments.length > 0) {


Readxmldemo read = new Readxmldemo (arguments[0]);


} else {


System.out.println ("Usage:java readxmldemo filename");


}


  }


Readxmldemo (String xmlFile) {


file input = new file (xmlFile);


SAXParserFactory factory = Saxparserfactory.newinstance ();


factory.setvalidating (TRUE);


try {


saxparser sax = Factory.newsaxparser ();


sax.parse (Input, New Xmlhandler ());


} catch (Parserconfigurationexception PCE) {


System.out.println ("Could not create that parser.");


System.out.println (Pce.getmessage ());


} catch (Saxexception Saxe) {


System.out.println ("Problem with the SAX parser.");


System.out.println (Saxe.getmessage ());


} catch (IOException IoE) {


System.out.println ("Error reading file.");


System.out.println (Ioe.getmessage ());


}


  }


}


class Xmlhandler extends DefaultHandler {


static int reading_name = 1;


static int reading_sex = 2;


static int reading_birth = 3;


static int reading_nothing = 0;


int currentactivity = reading_nothing;


ReadXml xml = new ReadXml ();


Xmlhandler () {


super ();


  }


public void Startelement (string uri, String localname, String qName, Attributes Attributes) {


 


if (qname.equals ("title"))


currentactivity = reading_name;


Else if (qname.equals ("file"))


currentactivity = reading_sex;


//else if (qname.equals ("Birth"))


//currentactivity = Reading_birth;


  }


public void characters (char[] ch, int start, int length) {


String value = new String (ch, start, length);


if (currentactivity = = reading_name)


xml.name = value;


if (currentactivity = = reading_sex)


xml.sex = value;


//if (currentactivity = = Reading_birth)


//xml.birth = value;


  }


public void EndElement (string uri, String localname, String qName) {


if (qname.equals ("page")) {


System.out.println ("\nname:" +xml.name);


System.out.println ("Sex:" +xml.sex);


//system.out.println ("Birth:" +xml.birth);


XML = new ReadXml ();


}


  }


}


class ReadXml {


String name;


String sex;


String Birth;


}


Saxparsepage.java


import javax.xml.parsers.*;


import org.xml.sax.*;


import org.xml.sax.helpers.*;


import java.io.*;


/**


* <p>title: Using sax to parse xml</p>


* <p>description: parsing book.xml files with Sax interfaces </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:SaxParsePage.java</p>


* @version 1.0


*/


public class Saxparsepage extends DefaultHandler {


/**


*<br> Method Description: Main method, declaring parse factory and loading XML file


*<br> input Parameters:


*<br> return type:


*/


public static void Main (string[] argv) {


try {


//Set up sax parsing factory


saxparserfactory spfactory = Saxparserfactory.newinstance ();


//Generate Sax parsing object


saxparser parser = Spfactory.newsaxparser ();


//Specify XML file for XML parsing


Parser.parse (New File ("Book.xml"), New Saxparsepage ());


} catch (Exception e) {


E.printstacktrace ();


  }


 }


/**


*<br> Method Description: Call
when file is open

*<br> input Parameters:


*<br> return type:


*/


public void Startdocument () {


System.out.println ("* * * began to parse * * *)";


 }


/**


*<br> Method Description: Called when a start tag is encountered


*<br> input Parameters:


*<br> return type:


*/


public void startelement (String uri,


String LocalName,


String QName,


Attributes Attributes) throws Saxexception {


System.out.println ("node start:" + qName);


for (int i=0;i<attributes.getlength (); i++) {


System.out.println ("Node property name:" + attributes.getqname (i));


System.out.println ("node attribute value:" +attributes.getvalue (i));


  }


 }


/**


*<br> Method Description: Called when the parser encounters a character that is not recognized as a tag or instruction type


*<br> input Parameters:


*<br> return type:


*/


public void characters (char[] ch,


int offset,


int length) throws saxexception{


SYSTEM.OUT.PRINTLN ("Node data:" + New String (ch, offset, length));


 }


/**


*<br> Method Description: When the node is encountered at the end of the call


*<br> input Parameters:


*<br> return type:


*/


public void EndElement (String uri,


String LocalName,


String qName) {


System.out.println ("End of Node:" + QName);


 }


/**


*<br> Method Description: When you call
at the end of the document

*<br> input Parameters:


*<br> return type:


*/


public void Enddocument () {


System.out.println (The "* * * * * * * * * * * * * *;


 }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.