Dom parsing XML simple example (Memo)

Source: Internet
Author: User
Tags xml example

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>

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.