The above is my final running result.
In order to consolidate the learning in the future, we will paste it here for future reference.
1. Library. xml:
<? XML version = "1.0" encoding = "gb2312"?>
<Books>
<Book email = "zhangyupeng_good">
<Name> JAVA </Name>
<Price> 100.00 </price>
</Book>
</Books>
2. domparse. java: (write it in Notepad.) import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstream; 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 domparse {public domparse () {documentbuilderfactory domfac = documentbuilderfactory. newinstance (); try {documentbuilder dombuilder = domfac. newdocumentbuilder (); inputstream is = new fileinputstream ("D:/zhangxin XML video tutorial/XML example and question/Java parsing XML document/library. XML "); document DOC = dombuilder. parse (is); element root = Doc. getdocumentelement (); nodelist books = root. getchildnodes (); If (books! = NULL) {for (INT I = 0; I <books. getlength (); I ++) {node book = books. item (I); If (book. getnodetype () = node. element_node) {string email = book. getattributes (). getnameditem ("email "). getnodevalue (); system. out. println (email); For (node = book. getfirstchild (); node! = NULL; node = node. getnextsibling () {If (node. getnodetype () = node. element_node) {If (node. getnodename (). equals ("name") {string name = node. getnodevalue (); string name1 = node. getfirstchild (). getnodevalue (); system. out. println (name); system. out. println (name1);} If (node. getnodename (). equals ("price") {string price = node. getfirstchild (). getnodevalue (); system. out. println (price) ;}}}}} catch (parser Configurationexception e) {e. printstacktrace ();} catch (saxexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} public static void main (string ARGs []) {New domparse () ;}} 3. compile: javac domparse. java 4. Run: Java domparse library. XML to see the first result. You will see a null value, which is caused by the following two lines of code: string name = node. getnodevalue (); ---> output null value string name1 = node. getfirstchild (). getnodevalue (); ---------> output Java, that is, the value we need