1.SAX
The full name simple API for XML, which is an interface, is also a package, as an interface,SAX is event-driven XML A standard interface for parsing.
It scans a line of XMLandparses it, then scans the next line and parses it.
The principle of 2.SAX
The 3.SAX implementation reads a string (the content is a character written after downloading from the network).
1. Get XmlReader:
// Create a saxparserfactory SAXParserFactory factory = saxparserfactory.newinstance (); = Factory.newsaxparser (). Getxmlreader ();
2. Set the content processor for XmlReader. (The content handler class needs to inherit the method in the override with another class)
// Set the content processor Reader.setcontenthandler (new Mycontenthandler ()) for XmlReader ;
3. Start parsing files (using XmlReader's Parse method)
// start parsing the file, ResultStr is the string we want to parse reader.parse (new InputSource (new StringReader (RESULTSTR))) ;
4.ContentHandler Class (Content processor)
1 //here inherit DefaultHandler, not contenthandler reason is2 //using the adapter mode, we need to use some of the ContentHandler methods3 //No extra methods are needed, and DefaultHandler just gives us the methods we need, no extra methods .4 Public classMycontenthandlerextendsDefaultHandler {5 String Hisname, address, money, sex, status;6 String TagName;7 8 Public voidStartdocument ()throwssaxexception {9System.out.println ("" "" "" "" "" "" ".);Ten } One A Public voidEnddocument ()throwssaxexception { -System.out.println ("" "" "" "" ""); - } the - Public voidstartelement (String NamespaceURI, String localname, -String QName, Attributes attr)throwssaxexception { -TagName =LocalName; + if(Localname.equals ("Worker")) { - //get all the properties of a label + for(inti = 0; I < attr.getlength (); i++) { ASystem.out.println (Attr.getlocalname (i) + "=" +Attr.getvalue (i)); at } - } - } - - Public voidendElement (String NamespaceURI, String localname, string qName) - throwssaxexception { in //after the Workr label is parsed, all the resulting data is printed -TagName = ""; to if(Localname.equals ("Worker")) { + This. PrintOut (); - } the } * Public voidCharacters (Char[] ch,intStartintlength) $ throwssaxexception {Panax Notoginseng if(Tagname.equals ("name")) -Hisname =NewString (CH, start, length); the Else if(Tagname.equals ("Sex")) +Sex =NewString (CH, start, length); A Else if(Tagname.equals ("status")) theStatus =NewString (CH, start, length); + Else if(Tagname.equals ("Address")) -Address =NewString (CH, start, length); $ Else if(Tagname.equals ("Money")) $Money =NewString (CH, start, length); - } - the Private voidprintout () { -System.out.print ("Name:");Wuyi System.out.println (hisname); theSystem.out.print ("Sex:"); - System.out.println (sex); WuSystem.out.print ("Status:"); - System.out.println (status); AboutSystem.out.print ("Address:"); $ System.out.println (address); -System.out.print ("Money:"); - System.out.println (money); - System.out.println (); A } + the}
android-file Analysis-< eight >