Dom methods read XML (infrequently used)

Source: Internet
Author: User

Book.java

 PackageCom.xml.demo; Public classBook {Private intID; PrivateString name; PrivateFloat money;  Public intgetId () {returnID; } @Override PublicString toString () {return"Book [id=" + ID + ", name=" + name + ", money=" + Money + "]"; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicFloat Getmoney () {returnMoney ; }     Public voidSetmoney (Float money) { This. Money =Money ; }     PublicBook () {//TODO auto-generated Constructor stub    }}

Demoserver.java

 PackageCom.xml.demo;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importorg.w3c.dom.Document;Importorg.w3c.dom.Element;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList; Public classDemoserver { PublicDemoserver () {//TODO auto-generated Constructor stub    }         PublicList<book> getbooks (InputStream inputstream)throwsException {List<Book> list=NewArraylist<book>(); //Create a document factoryDocumentbuilderfactory factory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Factory.newdocumentbuilder (); Document Document=Builder.parse (InputStream); Element element=document.getdocumentelement ();//get a stable element nodeNodeList booknodes=element.getelementsbytagname ("book");  for(intI=0;i<booknodes.getlength (); i++) {Element bookelement=(Element) Booknodes.item (i); Book Book=NewBook (); Book.setid (Integer.parseint (Bookelement.getattribute ("id"))); NodeList ChildNodes=bookelement.getchildnodes ();  for(intJ=0;j<childnodes.getlength (); j + +)            {                if(Childnodes.item (j). Getnodetype () = =Node.element_node) {                    if("Name". Equals (Childnodes.item (j). Getnodename ()))                    {Book.setname (Childnodes.item (j). Getfirstchild (). Getnodevalue ()); }Else if("Price". Equals (Childnodes.item (j). Getnodename ()))                    {Book.setmoney (Float.parsefloat (Childnodes.item (j). Getfirstchild (). Getnodevalue ()));        }}} list.add (book); }        returnlist; }    /**     * @paramargs*/     Public Static voidMain (string[] args) {String path= "Http://weikew.gotoip55.com/person.xml"; InputStream InputStream=httputils.getxml (path); Demoserver Server=NewDemoserver (); Try{List<Book> list=server.getbooks (InputStream);  for(book book:list) {System.out.println (book.tostring ()); }        } Catch(Exception e) {//Todo:handle Exception        }    }}

Httputils.java

 PackageCom.xml.demo;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL; Public classHttputils { Publichttputils () {//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; }}

Person.xml

<?XML version= "1.0" encoding= "Utf-8"?><Books>< BookID= " the"><name>Zhang Lao Jin</name>< Price>22.2</ Price></ Book>< BookID= " the"><name>Li Fa in</name>< Price>44.5</ Price></ Book></Books>

Dom methods read XML (infrequently used)

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.