Android Xmlpullparser Read XML file

Source: Internet
Author: User

As a result of the new Android, the development tool is Android Studio, the use of the eclipse of the development of Android, so for some say because the path of the 2 IDE is not able to read the file, do not analyze (in fact, can not analyze eclipse), This post is only for as users.

Our resource files, in addition to the Res directory, can also be placed in the Java, res sibling asserts (new) directory.

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/99/10/wKiom1lDfiCTkpzdAAAlgKMixsk107.png-wh_500x0-wm_ 3-wmp_4-s_2846464278.png "title=" qq picture 20170616144452.png "alt=" Wkiom1ldfictkpzdaaalgkmixsk107.png-wh_50 "/>

The XML to be parsed today is in this directory.

First of all, say xmlpullparser, this pull parsing is an event-based pattern. Like what

Read to the beginning of the XML declaration of content, then will trigger Start_document, return an int type data;

Read to the end of XML, then trigger End_document, return an int type data

Reads the start tag of the XML, returns START_TAG,

Reads the end tag of the XML, returns END_TAG,

Text that is read to XML returns text

After reading the label, you can get the label's property value and the text value

The following example illustrates the getweatherbycityname.xml of XML in an attachment.

Here are the steps to begin:

1. Get the input stream of the resource file inside the assets. Here in the as can be obtained by Assetmanager, that is, the way the first line of code. This input stream can also be obtained from the second row

2. Declare the Xmlpullparser and specify the encoding method for the input stream.

3. Define the type of event that a xmlpullparser gets to, EventType.

4, start the cycle of analysis, if not touched end_document, then go down; Take out the node is the text content of the tag of string and put into the list, as the back processing.

5, must note, must add eventtype=xmlpullparser.next (); otherwise the loop will always read the first node of the XML, that is, start_document without execution, leading to a dead loop.

InputStream InputStream = Getassets (). Open ("Getweatherbycityname.xml");//InputStream InputStream = GetClass (). getClassLoader (). getResourceAsStream ("assets/" + "Getweatherbycityname.xml"); Xmlpullparser Xmlpullparser = Xml.newpullparser (); Xmlpullparser.setinput (InputStream, "utf-8"); List<string> infoslist=new arraylist<string> (); int Eventtype=xmlpullparser.geteventtype (); while (eventtype!=xmlpullparser.end_document) {if ("string". Equals (Xmlpullparser.getname ())) {string Info=xmlpu         Llparser.nexttext ();     Infoslist.add (info); } eventtype=xmlpullparser.next (); }

Here is a more common XML format to read the contents of the file:

The XML corresponding to this XML is the Info2.xml in the attachment.


It is important to note that the XML file is properly formatted. Before due to copy from the online XML text content, the result of XML nameless various red lines, debugging the following program, the report XML format error, handwritten an XML, the compiler does not error, only debugging good.


The Studentbean is just a simple class with 3 private attributes, Id,name,age, with Get/set method

package com.yuanlp.pulldemo2;import android.os.bundle;import  android.support.v7.app.appcompatactivity;import android.util.xml;import  Com.yuanlp.pulldemo2.bean.studentbean;import org.xmlpull.v1.xmlpullparser;import java.io.inputstream ; import java.util.arraylist;import java.util.list;public class mainactivity extends  AppCompatActivity {     @Override     protected void  oncreate (bundle savedinstancestate)  {         Super.oncreate (savedinstancestate);         setcontentview ( R.layout.activity_main);        list<studentbean> list=click ();        for (studentbean student : list) {            system.out.println ("+++++++++<><>>>> >>>>>>>id is >>>>>>>>>>>>>>>>>>> >>>>> "+student.getid ());            System.out.println ("+++++++++<><>>>>>>>>>>> Age is >>>>> >>>>>>>>>>>>>>>>>>> "+student.getage ());            system.out.println ("+++++++++<><>>> >>>>>>>> name is >>>>>>>>>>>>>>>>>> >>>>>> "+student.getname ());       }     }    public list<studentbean> click () {         try {             Inputstream inputstream=getassets ().Open ("Info2.xml");            xmlpullparser  Xmlpullparser= xml.newpullparser ();             xmlpullparser.setinput (InputStream, "Utf-8");             StudentBean student=null;             List<StudentBean> studentlist=null;             int eventtype=xmlpullparser.geteventtype ();             while (eventtype!=xmlpullparser.end_document) {                 switch  (EventType) {                     case  xmlpullparser.start_document:                         studentlist=new ArrayList<StudentBean> ();                          break;                     case XmlPullParser.START_TAG:                         // From this start node to the next end node, the Read property belongs to a student, so declare a student here and put the data that this node takes into this student                           //is read by GetName () and then gets the value through Nexttext (), or gets the value of the property by Getattributevalue                          string tagname= Xmlpullparser.getname ();                         if  (tagname.equalsignorecase ("person")) {                              student=new studentbean ();                              student.setid (Xmlpullparser.getattributevalue (NULL, "id"));                          }else if  (student!=null) {                             if  ( Tagname.equalsignorecase ("name")) {                                  student.setname (Xmlpullparser.nexttext ());                              }else if  (Tagname.equalsignorecase ("Age")) {                                  student.setage (Xmlpullparser.nexttext ());                              }                         }                         break;                     case XmlPullParser.END_TAG:  End tag                          //If the end tag is read and the end tag is person, put the student into the list and place the student null Convenient Next loop re-declaration                          if  (Xmlpullparser.getname () equalsignorecase ("person") &&student!=null) {                             //read a person, you can put him into the list                               studentlist.add (student);                              student=null;                         }                 }                 eventtype=xmlpullparser.next ();             }               Inputstream.close ();nbsp;           return  studentlist;         } catch  (exception e)  {             e.printstacktrace ();         }        return null;    }}


This article is from the "Yuangushi" blog, make sure to keep this source http://cm0425.blog.51cto.com/10819451/1939165

Android Xmlpullparser Read XML file

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.