Android development tool Class 20_dompersonservice

Source: Internet
Author: User

XML file

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <persons> 3     <person id = "23" & Gt 4         <name>NEwii</name> 5         <age>21</age> 6     </person> 7      <person id = "> 8         <name>WiiWill</name> 9         <age >25</age>     </person> </persons>

DOM format

DOM parsing XML

1 ImportJava.io.InputStream;2 Importjava.util.ArrayList;3 Importjava.util.List;4 5 ImportJavax.xml.parsers.DocumentBuilder;6 Importjavax.xml.parsers.DocumentBuilderFactory;7 8 Importorg.w3c.dom.Document;9 Importorg.w3c.dom.Element;Ten ImportOrg.w3c.dom.Node; One Importorg.w3c.dom.NodeList; A  - ImportCom.wangjialin.domain.Person; -  the  Public classDompersonservice { -      -      Public StaticList<person> getpersons (InputStream instream)throwsexception{ -          +list<person> persons =NewArraylist<person>(); -Documentbuilderfactory factory =documentbuilderfactory.newinstance (); +Documentbuilder Builder =Factory.newdocumentbuilder (); ADocument document =Builder.parse (instream); atElement root =document.getdocumentelement (); -NodeList personnodes = root.getelementsbytagname ("Person"); -          -          for(inti=0; I < personnodes.getlength (); i++){ -              -Element personelement =(Element) Personnodes.item (i); in             intID =NewInteger (Personelement.getattribute ("id")); -Person person =NewPerson (); to Person.setid (ID); +NodeList childNodes =personelement.getchildnodes (); -              the              for(inty=0; Y < Childnodes.getlength (); y++){ *                 if(Childnodes.item (y). Getnodetype () = =Node.element_node) { $                     if("Name". Equals (Childnodes.item (y). Getnodename ())) {Panax NotoginsengString name =Childnodes.item (y). Getfirstchild (). Getnodevalue (); - person.setname (name); the}Else if("Age". Equals (Childnodes.item (y). Getnodename ())) { +String age =Childnodes.item (y). Getfirstchild (). Getnodevalue (); APerson.setage (NewShort (age)); the                     } +                 } -             } $ Persons.add (person); $         } - instream.close (); -         returnpersons; the     } -}

Person.java

1  Public classPerson {2     3     PrivateInteger ID;4     PrivateString name;5     PrivateShort age ;6 7      PublicPerson () {}8     9      PublicPerson (Integer ID, String name, short age) {Ten          This. ID =ID; One          This. Name =name; A          This. Age =Age ; -     } -      the      PublicPerson (String name, short age) { -          This. Name =name; -          This. Age =Age ; -     } +      -      PublicInteger getId () { +         returnID; A     } at  -      Public voidsetId (Integer id) { -          This. ID =ID; -     } -  -      PublicString GetName () { in         returnname; -     } to  +      Public voidsetName (String name) { -          This. Name =name; the     } *  $      PublicShort getage () {Panax Notoginseng         returnAge ; -     } the  +      Public voidSetage (short) { A          This. Age =Age ; the     } +  - @Override $      PublicString toString () { $         return"id=" + ID + ", name=" + name+ ", age=" +Age ; -     } -      the  -}

JUnit Test

1  Public voidTestdomgetpersons ()throwsthrowable{2         3InputStream instream = This. GetClass (). getClassLoader (). getResourceAsStream ("Wangjialin.xml");4list<person> persons =dompersonservice.getpersons (instream);5         6          for(person person:persons) {7 log.i (TAG, person.tostring ());8         }9}

Android development tool Class 20_dompersonservice

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.