Network XML parsing method
1 PackageCom.example.dashu_saxxml;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 ImportJava.util.HashMap;6 Importjava.util.List;7 8 ImportJavax.xml.parsers.SAXParser;9 Importjavax.xml.parsers.SAXParserFactory;Ten One ImportOrg.xml.sax.InputSource; A Importorg.xml.sax.SAXParseException; - - Public classSaxservice { the PublicSaxservice () { - } - - /** + * Network XML parsing method - * */ + Public StaticListReadXML ( A InputStream InputStream, String nodeName) { atListNULL; - Try { - //Create a parse XML factory object -SAXParserFactory saxparserfactory=saxparserfactory.newinstance (); -SAXParser Parser=saxparserfactory.newsaxparser ();//Parsing XML -MyHandler myhandler=NewMyHandler (nodeName); in Parser.parse (InputStream, MyHandler); -list=myhandler.getlist (); toInputstream.close ();//Close IO +}Catch(Exception e) { - //Todo:handle Exception the } * returnlist; $ }Panax Notoginseng}
MyHandler Inheritance DefaultHandler Parsing tool class
1 PackageCom.example.dashu_saxxml;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 Importjava.util.List;6 7 Importorg.xml.sax.Attributes;8 Importorg.xml.sax.SAXException;9 ImportOrg.xml.sax.helpers.DefaultHandler;Ten One ImportAndroid. R.integer; A - Public classMyHandlerextendsDefaultHandler { - Privatehashmap<string, string> map =NULL;//Store a single parsed full object thelistNULL;//storage So the parsing object - PrivateString Currenttag =NULL;//parsing element's label - PrivateString CurrentValue =NULL;//parsing value of element - PrivateString NodeName =NULL;//Current resolution node name + - PublicMyHandler (String nodeName) { + Super(); A This. NodeName =NodeName; at } - - PublicListgetList () { - returnlist; - } - in /** - * method to be called when XML file starts parsing to * */ + @Override - Public voidStartdocument ()throwssaxexception { the //TODO auto-generated Method Stub *List =NewArraylist(); $ Super. Startdocument ();Panax Notoginseng } - the /** + * Parse to Node start call method <name> A * */ the @Override + Public voidstartelement (String uri, String localname, String qName, -Attributes Attributes)throwssaxexception { $ //TODO auto-generated Method Stub $ if(Qname.equals (nodeName)) { -Map =NewHashmap<string, string>(); - } the if(Attributes! =NULL&& Map! =NULL) { - for(inti = 0; I < attributes.getlength (); i++) {Wuyi Map.put (Attributes.getqname (i), Attributes.getvalue (i)); the } - } WuCurrenttag =QName; - } About $ /** - * method called to parse the text of the middle clip to the end of the node - * */ - @Override A Public voidCharacters (Char[] ch,intStartintlength) + throwssaxexception { the //TODO auto-generated Method Stub - if(Currenttag! =NULL&& Map! =NULL) { $CurrentValue =NewString (CH, start, length); the if(CurrentValue! =NULL&&!currentvalue.trim (). Equals ("") the&&!currentvalue.trim (). Equals ("\ n")) { the map.put (Currenttag, currentvalue); the } - } inCurrenttag =NULL;//sets the value and label of the current node to null theCurrentValue =NULL; the } About the /** the * Parse to end of node call method </name> the * */ + @Override - Public voidendElement (String uri, String localname, string qName) the throwssaxexception {Bayi //when an end tag is encountered the if(Qname.equals (nodeName)) { the list.add (map); -Map =NULL; - } the } the}
Dashu.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<user-list>3<user id= "1" >4<name> Tree </name>5<age>22</age>6</user>7 8<user id= "2" >9<name> Plum </name>Ten<age>28</age> One</user> A -<user id= "3" > -<name> dad </name> the<age>48</age> -</user> - -<user id= "4" > +<name> mom </name> -<age>48</age> +</user> A</user-list>
"References"
http://blog.csdn.net/u010794950/article/details/24553733
Android development tool Class 13_ Saxservice