Pull parsing xml

Source: Internet
Author: User

Pullxmltools.java

 PackageCom.toolsdemo;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.xmlpull.v1.XmlPullParser;Importorg.xmlpull.v1.XmlPullParserFactory;/** mainly using pull to parse XML * @author Jack*/ Public classPullxmltools { PublicPullxmltools () {//TODO auto-generated Constructor stub    }         Public StaticList<person> Parsexml (InputStream inputstream,string encode)throwsException {List<Person> list=NULL; Person Person=NULL;//load parse each person node//Create an XML parsing factoryXmlpullparserfactory factory=xmlpullparserfactory.newinstance (); //get a reference to an XML parsing classXmlpullparser parser=Factory.newpullparser ();        Parser.setinput (Inputstream,encode); //Get Event Type        intEventtype=Parser.geteventtype ();  while(eventtype!=xmlpullparser.end_document) {            Switch(eventtype) { CaseXmlPullParser.START_DOCUMENT:list=NewArraylist<person>();  Break;  CaseXmlpullparser.start_tag:if("Person". Equals (Parser.getname ())) { person=NewPerson (); //Remove attribute value                    intId=integer.parseint (Parser.getattributevalue (0));                Person.setid (ID); }Else if("Name". Equals (Parser.getname ())) {String name=parser.nexttext ();//get the contents of this nodeperson.setname (name); }Else if("Age". Equals (Parser.getname ())) {                    intAge=Integer.parseint (Parser.nexttext ());                Person.setage (age); }                 Break;  CaseXmlpullparser.end_tag:if("Person". Equals (Parser.getname ()))                    {List.add (person); person=NULL; }                 Break; } EventType=Parser.next (); }        returnlist; }}

Person.java

 PackageCom.toolsdemo; Public classPerson {Private intID; PrivateString name; Private intAge ;  PublicPerson (intID, String name,intAge ) {        Super();  This. ID =ID;  This. Name =name;  This. Age =Age ; }     Public intgetId () {returnID; } @Override PublicString toString () {return"Person [id=" + ID + ", name=" + name + ", age=" + Age + "]"; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicPerson () {//TODO auto-generated Constructor stub    }}

Httputil.java

 PackageCom.toolsdemo;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classHttputil { PublicHttputil () {//TODO auto-generated Constructor stub    }         Public Staticinputstream GetXML (String path) {InputStream InputStream=NULL; Try{URL URL=NewURL (path); if(url!=NULL) {httpurlconnection connection=(HttpURLConnection) url.openconnection (); Connection.setconnecttimeout (3000); Connection.setdoinput (true); Connection.setrequestmethod ("GET"); intCode=Connection.getresponsecode (); if(code==200) {InputStream=Connection.getinputstream (); }                            }        } Catch(Exception e) {//Todo:handle Exception        }        returnInputStream; }    }

Test.java

 PackageCom.toolsdemo;ImportJava.io.InputStream;Importjava.util.List; Public classTest { PublicTest () {//TODO auto-generated Constructor stub    }    /**     * @paramargs*/     Public Static voidMain (string[] args) {String path= "Http://weikew.gotoip55.com/person.xml"; InputStream InputStream=httputil.getxml (path); Try{List<Person> List=pullxmltools.parsexml (InputStream, "Utf-8");  for(person person:list) {System.out.println (person.tostring ()); }        } Catch(Exception e) {//Todo:handle Exception        }    }}

Xml

<?XML version= "1.0" encoding= "Utf-8"?><Persons>< PersonID= " the"><name>Zhang Lao Jin</name>< Age>22</ Age></ Person>< PersonID= " the"><name>Li Fa in</name>< Age>44</ Age></ Person></Persons>

Xmlpullparser Bag

Pull parsing xml

Related Article

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.