Parsing of Android--xml files

Source: Internet
Author: User

Parsing methods: DOM, SAX, pull

    1. DOM: Converting XML to a tree for traversal

Public void domparser ()  {    try {        1. Creating Documentbuilder instances       DocumentBuilder builder =  Documentbuilderfactory.newinstance (). Newdocumentbuilder ();        // 2. Create a document instance Document doc = builder.parse (file);// 3. Gets the root node of the XML file (persons) element  Element = doc.getdocumentelement ();// 4. Gets the list of child nodes of the root node (a list of person) nodelist personlist =  element.getelementsbytagname ("person");// todo// nodelist personlist =  Element.getchildnodes ();//This method does not// 5.  traverse all the person nodes for  (int i = 0; i  < personlist.getlength ();  i++)  {// 6. Gets a single person node element person =  ( Element)  personlist.item (i);// 7. Reads the property string id = person.getattribute ("id") of the person; System.out.println ("person   id:  " + id"// 8. Gets a list of child nodes for person nodelist childlist =  Person.getchildnodes ();// 9. Traverse the person child node for  (int j = 0; j <  Childlist.getlength ();  j++)  {        // 10. Accessing child nodes Node  child =  (Node)  childlist.item (j);if  (Child.getnodetype ()  == node.element_ node)  {//  If the child node is an element node// 11. Gets the element name String name = child.getnodename ();if  (" Name ". Equals (name))  {//  get Person namesystem.out.println (" Name:  "         + child.getfirstchild (). Getnodevalue ());}  else if  ("Age". Equals (name))  {//  get Person agesystem.out.println ("Ages:"          + child.getfirstchild (). Getnodevalue ());}}}}  catch  (parserconfigurationexception e) &NBSP;{//&NBSP;TODO&NBSP;AUTO-GENERATED&NBSP;CATCH&NBSp;blocke.printstacktrace ();}  catch  (saxexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}

2.SAX: Traverse tags from top to bottom

 1. Creating a handler class, inheriting defaulthandlerclass saxhandler extends defaulthandler {/**  Previous node name  */private String tag;//  need to override five methods:// 1.startdocument (): Preprocessing// 2 when starting a document. Enddocument (): Finishing the document when you are finished// 3.startelement (): The start tag is called to call the method// 4.endelement (): Call the method// 5 when the closing tag is encountered. Characters (): Content public void startdocument ()  throws saxexception {system.out.println (" Call the Startdocument () method ");} Public void enddocument ()  throws saxexception {system.out.println ("Call EndDocument () method") );}   Read the property of the tag in this method Public void startelement (string uri, string localname,  string qname,attributes attributes)  throws saxexception {system.out.println (" Call the Startelement () method "+" LocalName: "+localname+" QName: "+qname";if  ("person". Equals (QName))  { System.out.println ("person:       "); System.out.println ("     id:&nbsP; "  + attributes.getvalue ("id"));} Tag = qname;} Public void endelement (String uri, string localname, string qname) throws  saxexception {system.out.println ("Call EndElement () method");}   reads the label content//  calls Public void characters each time it starts to end the tag (char[] ch, int start,  Int length) Throws saxexception {system.out.println ("Call characters () method"); String data = new string (ch, start, length);if  ("Name". Equals (tag))  { System.out.println ("name: "  + data);  else if  ("Age". Equals (Tag))  {system.out.println ("age: "  + data);}}  2.sax  parsing Public void saxparser ()  {try {//2.1 Creating a parser Javax.xml.parsers.SAXParser  parser = saxparserfactory.newinstance (). Newsaxparser ();//2.2 Create processor saxhandler handler  = new saxhandler ();//2.3 create file input stream FILEINPUTSTREAM&NBSP;FIS&NBSP;=&NBSP;NEW&NBsp FileInputStream (file);//2.4 parsing file Parser.parse (File, handler);}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (parserconfigurationexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (saxexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}

3.Pull: Similar to sax, with more applications in Android development

Public static void xmlpullparser (Inputstream is)  {try {// 1. Gets the input stream// 2. Gets the parser xmlpullparser parser = xml.newpullparser ();p arser.setinput (is,  "Utf-8");// 3. Gets the event type Int eventtype = parser.geteventtype ();// 4. Traversing node while (eventtype !=  xmlpullparser.end_document) {switch  (eventtype)  {case xmlpullparser.start_document://file open break; case xmlpullparser.start_tag://tag starts//4.1 gets the node name String name = parser.getname (); Name.equals ("person")) {//4.2  Gets the node attribute value string id = parser.getattributevalue (0);} Else if (name.equals ("name")) {//4.3  gets the next text value System.out.println ("name: " + parser.nexttext ());} Else if (Name.equals ("Age")) {System.out.println ("age: " + parser.nexttext ());} break;case xmlpullparser.end_tag://tag end Break;default:break;} Eventtype = parser.next ();} Is.close ();}  catch  (filenotfoundexception e) &NBSP;{//&NBSp Todo auto-generated catch blocke.printstacktrace ();}  catch  (xmlpullparserexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}


This article is from the "Androidcamera Summary" blog, please be sure to keep this source http://7183397.blog.51cto.com/7173397/1606124

Parsing of Android--xml files

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.