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)