Recursive Dom parsing (PARSE) XML

Source: Internet
Author: User

Friends. xml

 

<Span style = "font-size: 16px;"> <? XML version = "1.0" encoding = "UTF-8"?> </P> <p> <friends date = "2012"> </P> <p> <! -- Note --> </P> <p> <friend ID = "1"> </P> <p> <Name> Xiaohong </Name> <br/> <QQ> 123456789 </QQ> </P> <p> </friend> </P> <p> <Name> Xiaoping </Name> <br/> <QQ> 12345678 </QQ> </P> <p> </friend> </P> <p> <friend> </P> <p> <name Mark = "hello"> Min </Name> <br/> <QQ> 1234567 </QQ> </P> <p> </friend> </P> <p> <friend> </P> <p> <Name> xiaoliang </Name> <br/> <QQ> 987654321 </QQ> </P> <p> </friend> </P> <p> <Name> Xiao Jian </Name> <br/> <QQ> 98765432 </QQ> </P> <p> </friend> </P> <p> </friends> </P> <p> </span>

 

Dom Parse XML

 

<Span style = "font-size: 16px;"> Import Java. io. file; </P> <p> Import javax. XML. parsers. documentbuilderfactory; <br/> Import javax. XML. parsers. documentbuilder; </P> <p> Import Org. w3C. dom. element; <br/> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. namednodemap; <br/> Import Org. w3C. dom. nodelist; <br/> Import Org. w3C. dom. ATTR; <br/> Import Org. w3C. dom. node; <br/> Import Org. w3C. dom. comment; </P> <p> // uses recursive Dom to parse any XML document and load the XML document to the memory, which occupies a large amount of memory, random Access </P> <p> public class recursiondom_xml <br/> {<br/> Public static void main (string [] ARGs) throws exception <br/> {<br/> // 1. get Dom parser factory <br/> documentbuilderfactory DBF = documentbuilderfactory. newinstance (); </P> <p> // 2. obtain the specific Dom parser <br/> documentbuilder DB = DBF. newdocumentbuilder (); </P> <p> // obtain the root node of the XML document <br/> document = dB. parse (new file ("friends. XML "); </P> <p> // get the root element node of the document <br/> element E = document. getdocumentelement (); </P> <p> parsexml (E); </P> <p >}</P> <p> Public static void parsexml (element) <br/>{< br/> // obtain the information of the element node <br/> string name = element. getnodename (); <br/> system. out. print ("<" + name); </P> <p> // obtain the attributes of this element node <br/> namednodemap map = element. getattributes (); </P> <p> // obtain the child nodes (including spaces) under the current element node </span><Span style = "font-size: 16px;"> nodelist child = element. getchildnodes (); </P> <p> If (null! = Map) <br/>{< br/> for (INT I = 0; I <map. getlength (); I ++) <br/>{< br/> ATTR = (ATTR) map. item (I); </P> <p> string name1 = ATTR. getname (); <br/> string value1 = ATTR. getvalue (); <br/> system. out. print ("" + name1 + "= \" "+ value1 + "\""); <br/>}</P> <p> system. out. print (">"); </P> <p> for (Int J = 0; j <child. getlength (); j ++) <br/>{< br/> node = child. item (j); </P> <p> // determines whether the node is an element. Node, recursive intersection <br/> If (node. getnodetype () = node. element_node) <br/>{< br/> // start recursion <br/> parsexml (element) node ); <br/>}</P> <p> // determines whether the node is text or not. Recursive exit <br/> else if (node. getnodetype () = node. text_node) <br/>{< br/> system. out. print (node. getnodevalue () + ""); </P> <p >}</P> <p> // determines whether the node is annotated. <br/> else if (node. getnodetype () = node. comment_node) <br/>{< br/> system. out. print ("<! -- "); <Br/> Comment comment = (comment) node; <br/> // comment content <br/> string data = comment. getdata (); </P> <p> system. out. print (data); <br/> system. out. print ("-->"); </P> <p >}< br/>}</P> <p> system. out. println ("</" + name + ">"); </P> <p >}</span>


 

 

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.