The following is a simple XML example for Dom parsing. The main purpose of this example is Memo.
Package COM. linming. dom. XML; import Java. io. file; import Java. io. ioexception; 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. nodelist; import Org. XML. sax. saxexception;/*** @ author Lin Ming * @ create_date 09:31:46 * @ Description */public class domdemo {public static void main (string [] ARGs) {try {// obtain the documentbuilderfactory factory = documentbuilderfactory object of the documentbuilderfactory class. newinstance (); // obtain the documentbulder Class Object documentbuilder builder = factory. newdocumentbuilder (); try {document = builder. parse (new file ("E:" + file. separator + "test. XML ");/** // get the Name node. The following figure shows how to get a single node nodelist = * document. getelementsbytagname ("name"); * system. out. println ("name =" + * nodelist. item (0 ). getfirstchild (). getnodevalue (); * // obtain all addresslinked and retrieve each name and email nodelist = document. getelementsbytagname ("linked"); For (INT I = 0; I <nodelist. getlength (); I ++) {Element E = (element) nodelist. item (I); system. out. println ("name:" + E. getelementsbytagname ("name "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("Email:" + E. getelementsbytagname ("email "). item (0 ). getfirstchild (). getnodevalue () ;}} catch (saxexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} catch (parserconfigurationexception e) {e. printstacktrace ();}}}
Path of the following file: e: \ test. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Addresslinked> <linked> <Name> Lin Ming </Name> <email> linming@126.com <email> </linked> <Name> Lin min </Name> <email> linmin@126.com <email> </linked> </addresslinked>