First, XML parsing:
1, Pull analytic mode:
1) First obtains an instance of Xmlpullparserfactory, and obtains the Xmlpullparser object with this example;
2) Call Xmlpullparser's SetInput () method to set the XML data returned by the server;
3) through the Geteventtype () method can get the current parse event, you can use the loop to parse, if the current parse event is not equal to xmlpullparser.end_document, stating that the parsing work is not completed, call next () method to obtain the next parse event.
Example code:
Private voidparsexmlwithpull (String xml) {Try{xmlpullparserfactory Factory=xmlpullparserfactory.newinstance (); Xmlpullparser Parser=Factory.newpullparser (); Parser.setinput (NewStringReader (XML)); //Get node type intEventType =Parser.geteventtype (); while(EventType! =xmlpullparser.end_document) { //get current parse node nameString NodeName =Parser.getname (); Switch(eventtype) {//start parsing a node. CaseXmlpullparser.start_tag: Break; //finish parsing a node. CaseXmlpullparser.end_tag: Break; default: Break; } parser.next (); } }Catch(Exception e) {e.printstacktrace (); }}
2, Sax parsing mode:
1) Create a new class to inherit from DefaultHandler;
2) Rewrite the following 5 methods:
--startdocument (): Called at the beginning of XML parsing;
--startelement (): Called at the beginning of parsing a node;
--characters (): Called when the contents of the node are obtained;
--endelement (): Called when parsing a node is completed;
--enddocument (): Called when the entire XML parsing is complete.
Public classContentHandlerextendsDefaultHandler {@Override Public voidStartdocument ()throwssaxexception {Super. Startdocument (); } /**The parameter inside the method is the information of the parsed node, such as LocalName.*/@Override Public voidStartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {Super. Startelement (URI, LocalName, qName, attributes); } @Override Public voidCharacters (Char[] ch,intStartintLengththrowssaxexception {Super. Characters (ch, start, length); } @Override Public voidEndElement (String uri, String localname, String qName)throwssaxexception {Super. EndElement (URI, LocalName, QName); } @Override Public voidEnddocument ()throwssaxexception {Super. Enddocument (); }}
3) Create SAXParserFactory instance: SAXParserFactory factory = Saxparserfactory.newinstance ();
4) Create XMLReader object: XMLReader XMLReader = Factory.newsaxparser (). Getxmlreader ();
5) Create a custom ContentHandler object;
6) Set the instance of ContentHandler to XMLReader;
7) Call XMLReader's Parse () method to begin parsing: Xmlreader.parse (new InputSource (new StringReader (XmlData)));
code example:
private void Parsexmlwithsax (String xml) { try {saxparserfactory factory = Saxparserfactory.newinstance (); SAXParser parser = Factory.newsaxparser (); XMLReader reader = Parser.getxmlreader (); ContentHandler handler = new new InputSource (new StringReader (XML)); catch (Exception e) {E.printstacktrace (); }}
Second, JSON parsing:
1, using Jsonobject analysis:
Private void Parsejsonwithjsonobject (String json) { try{ new Jsonarray (JSON); for (int i = 0; i < jsonarray.length (); i++) { = Jsonarray.getjsonobject (i); = jsonobject.getstring ("Data"); ......; } } Catch (Exception e) { e.printstacktrace (); }}
2. Using Gson parsing: Gson parsing JSON is simpler
1) need to import jar package before using Gson: Gson-2.2.4.jar
2) Gson can automatically map a JSON-formatted string into an object, eliminating the need for manual parsing;
Private void Parsejsonwithgson (String jsondata) { new Gson (); ListNew typetoken<List<Object>>() {}.gettype ()); for (Object obj:data) { ...; }}
3) If the parse is not a JSON array, it can be written directly: ObjectType type = Gson.fromjson (Jsondata, Objecttype.class);
android--Data parsing