mysaxhandler***
PackageAndroid_xml_sax;importJava.util.arraylist;importJava.util.hashmap;importJava.util.list;importOrg.xml.sax.attributes;importOrg.xml.sax.saxexception;importOrg.xml.sax.helpers.defaulthandler;public class Mysaxhandler extendsDefaultHandler {public list<book>list; PublicBook book; PublicStringBuilder Builder; Public list<book>GetList () {returnList } @Override public void Startdocument () throwssaxexception {//TODO auto-generated method stub super. Startdocument (); List = new arraylist<book>(); Builder = newStringBuilder (); }/** * Method Description: * The parser calls this method at the beginning of each element in the XML document, and there will be a corresponding EndElement event for each startelement event (even if the element is empty). The contents of all elements are reported sequentially before the corresponding EndElement event. * Parameter Description: *uri-namespace URI, if the element does not have a namespace URI, or if namespace processing is not performed, an empty string *localname-local name (without prefix), or an empty string *qname-qualified name (prefixed) If namespace processing is not performed, if The qualified name is not available, then an empty string *atts-connects to the attribute on the element. If there is no property, it will be an empty Attributes object. After Startelement returns, the value of this object is undefined */@Override public voidStartelement (String uri, String localname, String qName, Attributes Attributes) throwssaxexception {//TODO auto-generated method stub super. startelement (URI, LocalName, qName, attributes); if (Qname.equals ("book") {book = newBook (); } builder.setlength (0); /** * to receive notification of character data, you can create parsed string literals through the new string (Ch,start,length) constructor. * Parameters: *ch-character *start from XML document-start position in array *length-the number of characters read from the array */@Override public void characters (char[] ch, int start, intLength) throwssaxexception {//TODO auto-generated method stub super. characters (ch, start, length); Builder.append (CH, start, length); Appends the read character array to the builderThe /** * SAX Parser calls this method at the end of each element in the XML document, and for each endElement event there will be a corresponding Startelement event (even if the element is empty). * Parameters: *uri-namespace URI, if the element does not have a namespace URI, or if namespace processing is not performed, an empty string *localname-local name (without prefix), or an empty string *qname-qualified XML name (prefixed with prefix) if namespace processing is not performed ), an empty string if the qualified name is not available * /@Override public void endElement (string uri, String localname, String qName) throws Saxex ception {//TODO auto-generated method stub Super. EndElement (URI, LocalName, QName), if (Qname.equals ("id")) {Book.setid ( Integer.parseint (Builder.tostring ())); }else if (qname.equals ("name")) {Book.setname (builder.tostring ());} else if (qname.equals ("price")) {Book.setprice (Float.parsefloat (builder.tostring ()));} else if (qname.equals ("book")) {list.add (book),}} @Override public void Enddocument () throws Saxexception {//To Do auto-generated method stub Super. Enddocument ();}}
test.java***
PackageAndroid_xml_sax;importJava.io.file;importJava.io.ioexception;importJava.util.list;importJavax.xml.parsers.parserconfigurationexception;import javax.xml.parsers.saxparser;import Javax.xml.parsers.saxparserfactory;import Org.xml.sax.saxexception;public class Test {public static void main (string[] args) {//TODO auto-generated method stub SAXParserFactory factory = saxparserfactory.newinstance (); try {saxparser parser = Factory.newsaxparser (); Mysaxhandler handler = new Mysaxhandler () parser.parse (New File ("Src/xml/book.xml" ), handler); list<book> list = handler.getlist (); for (book book:list) {System.out.println (book.tostring ());}} Catch (Parserconfigurationexception e) {//TODO auto-generated catch block e.printstacktrace ();} catch (Saxexceptio n e) {//TODO auto-generated catch block e.printstacktrace ()} catch (IOException e) {//TODO auto-generated catch block E.prin Tstacktrace (); } }}
book.java***
Package Android_xml_sax;public class book { private int ID; Private String name; private float price ; public int GetId () {return ID;} public void SetId (int. ID) {this.id = ID;} public String Getnam E () {return name;} public void setName (String name) {this.name = name;} public float GetPrice () { Return price , public void Setprice (float price ) {This.price = Price ,} @Override public String toSt Ring () {return ' ID: ' + ID + ', Name: ' + name + ', Price: ' + Price ;}}
book.xml***
<?xml version= "1.0" encoding= "Utf-8"?><books> <book> <id>1</id> < Name>thinking in java</name> <price>80.00</price> </book> <book> <id>2</id> <name>core java</name> <price>90.00</price> </book> <book> <id>3</id> <name>hello, andriod</name> <price>100.00</price> </book></books>
Android parsing XML sax