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 +"] ";}}