Java Dom instance (w3cdom)

Source: Internet
Author: User
Package anby.com; import Java. io. file; import Java. io. ioexception; import Java. util. arraylist; import Java. util. list; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import javax. XML. parsers. parserconfigurationexception; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; import Org. XML. sax. saxexception; public class readxml {public static void main (string ARGs []) throws parserconfigurationexception, saxexception, ioexception {book = New Book (); List books = new arraylist (); // obtain the factory instance documentbuilderfactory factory = documentbuilderfactory. newinstance (); factory. setignoringelementcontentwhitespace (true); // obtain the documentbuilder instance documentbuilder dbuilder = factory through the factory. newdocumentbuilder (); // get Dom document = dbuilder. parse ("book. XML "); // obtain the following subnode nodelist bookelement = document. getelementsbytagname ("book"); // traverse nodelist values for (INT I = 0; I <bookelement. getlength (); I ++) {node = bookelement. item (I); // obtain the element bookele = (element) node under the node; // convert the Node object to the element book. setid (bookele. getattribute ("ID"); // get the property nodelist childlist = node. getchildnodes (); For (Int J = 0; j <childlist. getlength (); j ++) {// If (childlist. item (j ). getnodename () = "title") // This prevents NULL pointer exceptions if ("titile ". equals (childlist. item (j ). getnodename () // node. getnodename () Get the node name {book. settitile (childlist. item (j ). gettextcontent (); // node .. gettextcontent () Get node content} If ("price ". equals (childlist. item (j ). getnodename () {book. setprice (childlist. item (j ). gettextcontent () ;}} system. out. println (book) ;}} class book {string ID; string titile; string price; Public String GETID () {return ID;} public void setid (string ID) {This. id = ID;} Public String gettitile () {return titile;} public void settitile (string tititile) {This. titile = titile;} Public String getprice () {return price;} public void setprice (string price) {This. price = Price ;}@ overridepublic string tostring () {return "Book [ID =" + ID + ", titile =" + titile + ", price = "+ Price +"] ";}}

  

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.