Android Learning Note-xml processing

Source: Internet
Author: User

How SAX works: When you scan to the beginning and end of documents (document), the beginning and end of elements (element), the end of documents (document), and so on, the time-processing function of the comrade, the time-processing function to do the corresponding action, and then continue the same scan until the end of the document.

Download XML code from the Network for processing

How to read the contents of a network File reference http://shamrock.blog.51cto.com/2079212/1580269

Test.xml

<?xml version= "1.0" encoding= "UTF-8"? ><workers><worker id= "A01" ><name>Tom</name> <sex> male </sex><status> manager </status><address> Hubei </address><money>20000< /money></worker><worker id= "A02" ><name>Tom1</name><sex> male </sex><status > Staff </status><address> Hubei Wuhan </address><money>22000</money></worker>< Worker id= "A03" ><name>Tom2</name><sex> men </sex><status> Employees </status>< Address> Yichang </address><money>20200</money></worker></workers>, Hubei


The download file cannot be performed in the main thread.

Class myhandler extends handler {public myhandler ()  {}public myhandler ( Looper looper)  {super (Looper);} @Overridepublic  void handlemessage (message msg)  {super.handlemessage (msg); Bundle bundle = msg.getdata (); String urlstr = bundle.getstring ("Urlstr"); String filename = bundle.getstring ("FileName"); String path = bundle.getstring ("path"); Httpdownloader httpdownloader = new httpdownloader ();//Save the contents of the read XML file to result string  Result = httpdownloader.download (URLSTR); SYSTEM.ERR.PRINTLN (result); Toast.maketext (mainactivity.this,  "~",  toast.length_short). Show ();try {//  Create a saxparserfactorysaxparserfactory factory = saxparserfactory.newinstance (); Xmlreader reader = factory.newsaxparser (). Getxmlreader ();//  Set Content Processor Reader.setcontenthandler (New mycontenthandler ()) for XmlReader;//  start parsingPiece Reader.parse (New inputsource (New stringreader (Result));}  catch  (exception e)  {e.printstacktrace ();}}


Mycontenthandler.java

Package com.example.xml;import org.xml.sax.attributes;import org.xml.sax.saxexception;import  org.xml.sax.helpers.DefaultHandler;public class MyContentHandler extends  defaulthandler {string hisname, address, money, sex, status; String tagname;public void startdocument ()  throws saxexception { System.out.println ("" "" "" "" "" "" ""; Public void enddocument ()  throws saxexception {system.out.println ("" "" "" "End" " `");} Public void startelement (String namespaceuri, string localname,string qname, &NBSP;ATTRIBUTES&NBSP;ATTR)  throws SAXException {tagName = localName;if  ( Localname.equals ("worker"))  {//gets the full property of the label for  (int i = 0; i <  Attr.getlength ();  i++)  {system.out.println (Attr.getlocalname (i)  +  "="  +  Attr.getvalue (i));}} Public voiD endelement (String namespaceuri, string localname, string qname) throws  saxexception {//all obtained data tagname =  "";if  (Localname.equals ("worker") will be printed after parsing the WORKR tag.  {this.printout ();}} Public void characters (char[] ch, int start, int length) throws  saxexception {if  (tagname.equals ("name")) hisname = new string (ch, start,  Length);else if  (tagname.equals ("Sex")) sex = new string (ch, start, length); else if  (Tagname.equals ("status")) status = new string (ch, start, length); else if  (Tagname.equals ("Address")) address = new string (ch, start, length); else if  (Tagname.equals ("Money")) money = new string (ch, start, length); Private void printout ()  {system.out.print ("name: "); System.out.println (Hisname); System.out.print ("sex: "); SYSTEM.OUT.PRINTLN (Sex); System.out.print ("status: "); SYSTEM.OUT.PRINTLN (status); System.out.print ("address: "); SYSTEM.OUT.PRINTLN (address); System.out.print ("money: "); SYSTEM.OUT.PRINTLN (Money); System.out.println ();}}



This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1582142

Android Learning Note-xml processing

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.