Parse xml using the pull interpreter in android

Source: Internet
Author: User

Android has integrated the pull interpreter, so you do not need to add a jar file. If you use pull in Java EE, You need to manually add the pull jar file. The android system uses the pull interpreter to process xml files. Therefore, pull is superior.
1. main. xml
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">

<ListView
Android: id = "@ + id/lv"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>

</LinearLayout>

2. item. xml
<TextView xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

3. I saved the person. xml file to the assets folder of android.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> jack </name>
<Age> 16 </age>
</Person>
<Person id = "24">
<Name> tom </name>
<Age> 15 </age>
</Person>
</Persons>
4. MainActivity. java
Public class MainActivity extends Activity {

Private PersonService ps = null;

Private ListView lv;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Ps = new PersonService ();
Lv = (ListView) this. findViewById (R. id. lv );
Try {
AssetManager am = getAssets ();
InputStream is = am. open ("person. xml ");
List <Person> list = ps. getPersons (is );
List <String> names = new ArrayList <String> ();
For (Person p: list ){
String name = p. getName ();
Names. add (name );
System. out. println (name );
}
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, R. layout. item, 0, names );
Lv. setAdapter (adapter );
} Catch (Exception e ){
E. printStackTrace ();
}

}
}

 

5. PersonService. java
Public class PersonService {

Public List <Person> getPersons (InputStream xml) throws Exception {
XmlPullParser pullParser = Xml. newPullParser ();
List <Person> persons = null;
Person person = null;
PullParser. setInput (xml, "UTF-8 ");
Int event = pullParser. getEventType ();

// Determine whether to explain to the end of the document
While (event! = XmlPullParser. END_DOCUMENT ){
Switch (event ){

Case XmlPullParser. START_DOCUMENT:
Persons = new ArrayList <Person> ();
Break;
Case XmlPullParser. START_TAG:
If ("person". equals (pullParser. getName ())){
// Obtain the value of the first parameter in the tag.
Int id = Integer. parseInt (pullParser. getAttributeValue (0 ));
Person = new Person ();
Person. setId (id );
}
If ("name". equals (pullParser. getName ())){
// Obtain the text content in the middle of the tag
String name = pullParser. nextText ();
Person. setName (name );
}
If ("age". equals (pullParser. getName ())){
Int age = Integer. parseInt (pullParser. nextText ());
Person. setAge (age );
}
Break;

Case XmlPullParser. END_TAG: // If the explanation label is perosn, the person object will be stored in the list.
If ("person". equals (pullParser. getName ())){
 

Persons. add (person );
}
Break;

Case XmlPullParser. END_DOCUMENT:

Break;

Default:
Break;
}
// Pull does not take the initiative to perform backward execution, so you can call the next method and pay the value to the event
Event = pullParser. next ();
}
System. out. println ("persons. size () -->" + persons. size ());


Return persons;
}
}

 


6. Person. java
Public class Person {

Private Integer id;
Private String name;
Private Integer age;


Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public Integer getAge (){
Return age;
}
Public void setAge (Integer age ){
This. age = age;
}


}

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.