Parse XML with Dom

Source: Internet
Author: User
Tags tagname

Example 1:

Package COM. shengsiyuan. XML. dom; import Java. io. file; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. nodelist; public class domtest1 {public static void main (string [] ARGs) throws exception {// Step 1: obtain the DOM parser factory (which is used to create a specific parser) documentbuilderfactory DBF = documentbuilderfactory. newinstance (); // system. out. println ("Class Name:" + DBF. getclass (). getname (); // Step 2: Obtain the specific Dom parser documentbuilder DB = DBF. newdocumentbuilder (); // system. out. println ("Class Name:" + dB. getclass (). getname (); // Step 3: parse an XML document and obtain the Document Object (root node) document = dB. parse (new file ("candidate. XML "); nodelist list = document. getelementsbytagname ("person"); For (INT I = 0; I <list. getlength (); I ++) {element = (element) list. item (I); string content = element. getelementsbytagname ("name "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("name:" + content); content = element. getelementsbytagname ("Address "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("Address:" + content); content = element. getelementsbytagname ("tel "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("Tel:" + content); content = element. getelementsbytagname ("fax "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("Fax:" + content); content = element. getelementsbytagname ("email "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("Email:" + content); system. out. println ("--------------------------------------");}}}

Example 2:

Package COM. shengsiyuan. XML. dom; import Java. io. file; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. namednodemap; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; public class domtest2 {public static void main (string [] ARGs) throws exception {documentbuilderfactory DBF = documentbuilderfactory. newinstance (); documentbuilder DB = DBF. newdocumentbuilder (); document DOC = dB. parse (new file ("student. XML "); // system. out. println (Doc. getxmlencoding (); // system. out. println (Doc. getxmlversion (); // system. out. println (Doc. getxmlstandalone (); // obtain the root element node of the document. Element root = Doc. getdocumentelement (); system. out. println (root. gettagname (); nodelist list = root. getchildnodes (); system. out. println (list. getlength (); For (INT I = 0; I <list. getlength (); I ++) {system. out. println (list. item (I ). getnodename ();} system. out. println ("--------------------------------"); For (INT I = 0; I <list. getlength (); I ++) {node n = List. item (I); system. out. println (N. getnodetype () + ":" + N. getnodevalue ();} system. out. println ("--------------------------------"); For (INT I = 0; I <list. getlength (); I ++) {node n = List. item (I); system. out. println (N. gettextcontent ();} system. out. println ("----------------------------------"); nodelist = Doc. getelementsbytagname ("student"); For (INT I = 0; I <nodelist. getlength (); I ++) {namednodemap NNM = nodelist. item (I ). getattributes (); string attrname = NNM. item (0 ). getnodename (); system. out. print (attrname); system. out. print ("="); string attrvalue = NNM. item (0 ). getnodevalue (); system. out. println (attrvalue );}}}

Example 3:

Package COM. shengsiyuan. XML. dom; import Java. io. file; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. w3C. dom. ATTR; import Org. w3C. dom. comment; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. namednodemap; import Org. w3C. dom. node; import Org. w3C. dom. nodelist;/*** use recursion to parse any given XML document and output its content to the command line * @ author zhanglong **/Public C Lass domtest3 {public static void main (string [] ARGs) throws exception {documentbuilderfactory DBF = documentbuilderfactory. newinstance (); documentbuilder DB = DBF. newdocumentbuilder (); document DOC = dB. parse (new file ("student. XML "); // obtain the root element node element root = Doc. getdocumentelement (); parseelement (Root);} Private Static void parseelement (element) {string tagname = element. getnodename (); nodelist ch Ilease = element. getchildnodes (); system. out. print ("<" + tagname); // namednodemap object composed of all the attributes of the element, which must be judged by namednodemap map = element. getattributes (); // If the element has an attribute if (null! = Map) {for (INT I = 0; I <map. getlength (); I ++) {// obtain each attribute of this element ATTR = (ATTR) map. item (I); string attrname = ATTR. getname (); string attrvalue = ATTR. getvalue (); system. out. print ("" + attrname + "= \" "+ attrvalue +" \ "") ;}} system. out. print (">"); For (INT I = 0; I <children. getlength (); I ++) {node = children. item (I); // obtain the node type short nodetype = node. getnodetype (); If (nodetype = node. element_node) {// The Element, Continue recursion parseelement (element) node);} else if (nodetype = node. text_node) {// recursively exports system. out. print (node. getnodevalue ();} else if (nodetype = node. comment_node) {system. out. print ("<! -- "); Comment comment = (comment) node; // comment content string data = comment. getdata (); system. out. print (data); system. out. print ("-->") ;}} system. out. print ("</" + tagname + "> ");}}

  

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.