First of all
Prepare an XML file for parsing, where we assume that the file name to parse is Person.xml, and the specific contents of the file are:
<?xml version="1.0" encoding="utf-8"?><persons> <person id="23"> <name>liming</name> <age>23</age> </person></persons>
Next write a person's JavaBean:
PackageCom.yangzi.domain; Public class person { Private intIdPrivateString name;Private intAge Public int getId() {returnId } Public void setId(intID) { This. id = ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }@Override PublicStringtoString() {return "person [id="+ ID +", name="+ name +", age="+ Age +"]"; }}
And then
Is the writing of the Business class:
PackageCom.yangzi.service;ImportJava.io.InputStream;ImportJava.util.ArrayList;ImportJava.util.List;ImportOrg.xmlpull.v1.XmlPullParser;ImportANDROID.UTIL.XML;ImportCom.yangzi.domain.Person; Public class personservice { Public StaticList<person>getpersons(InputStream XML)throwsException {list<person> persons =NULL; Person person =NULL;//Get Xmlpull parserXmlpullparser Pullparser = Xml.newpullparser ();//Setup parser to parse the content, and encode the wayPullparser.setinput (XML,"Utf-8");//loop to read XML file node intevent = Pullparser.geteventtype ();//Get a node in the XML file while(Event! = xmlpullparser.end_document) {Switch(event) {//xml File Start node, start node generally do the initialization of data CaseXmlPullParser.START_DOCUMENT:persons =NewArraylist<person> (); Break;//If the start node CaseXmlpullparser.start_tag://If the person node if("Person". Equals (Pullparser.getname ())) {//Get IDperson =NewPerson ();//int id = integer.parseint (pullparser.getattributevalue (0)); //int id = new Integer (pullparser.getattributevalue (0)); intid = integer.valueof (pullparser.getattributevalue (0)); Person.setid (ID); }//If the name node if("Name". Equals (Pullparser.getname ())) {String name = Pullparser.nexttext (); Person.setname (name); }//If the age node if("Age". Equals (Pullparser.getname ())) {//int age = Integer.parseint (Pullparser.nexttext ()); //int age = New Integer (Pullparser.nexttext ()); intAge = Integer.valueof (Pullparser.nexttext ()); Person.setage (age); } Break; CaseXmlpullparser.end_tag:if("Person". Equals (Pullparser.getname ())) {Persons.add (person); person =NULL; } Break; } event = Pullparser.next ();//Read Next node}returnPersons }}
Next
You can write a test class to test it:
PackageCom.yangzi.test;ImportJava.io.InputStream;ImportJava.util.List;ImportAndroid.test.AndroidTestCase;ImportAndroid.util.Log;ImportCom.yangzi.domain.Person;ImportCom.yangzi.service.PersonService; Public class personservicetest extends androidtestcase { Private Static FinalString TAG ="Personservicetest"; Public void testpersons()throwsexception{InputStream in = This. GetClass (). getClassLoader (). getResourceAsStream ("Person.xml"); list<person> persons = Personservice.getpersons (in); for(Person Per:persons) {log.i (TAG, per.tostring ()); } }}
Before the test
To introduce a test environment in the project manifest file:
<manifest xmlns:android="Http://schemas.android.com/apk/res/android"><application>.....<uses-library android:name="Android.test.runner" /><!--Note the location of the placement--....</Application>....<instrumentationandroid:name="Android.test.InstrumentationTestRunner" Android:targetpackage="Com.yangzi.xml" /> <!--Note the location of the placement--</manifest>
OK, click on the Testpersons method and run the test for Android Junit test!
Experience
According to the Business class Personservice analysis code, it can be seen that in the XML file does not operate, can be arbitrarily named, that is, in parsing the XML file can be directly extracted from their own want to do, the other do not operate.
Own idea, do not know to, please the big god correct me!
The pull parser in android resolves an XML file case