XML-dom parsing XML

Source: Internet
Author: User
1. How XML is parsed using dom
  • Based on the DOM (Document Object Model) parsing method, the entire XML file is loaded into the memory and converted into a DOM tree, therefore, applications can randomly access any data in the DOM tree;
  • Advantages: high flexibility and fast speed;
  • Disadvantage: resource consumption is high;
2. Elements vs nodes (nodes contain elements)
  • Nodes include element nodes, attribute nodes, and text nodes.
    <! -- Students. xml -->
    <? XML version = "1.0" encoding = "UTF-8"?> <Students> <student> <name ID = "001" xx = "Haha"> Michael Zhang </Name> <sex> male </sex> <age> 20 </age> </student> <name ID = "002"> Li Si </Name> <sex> female </sex> <age> 21 </age> </student> </students> // element node: student, name, sex, age // attribute node: ID, AA // text node: James, male, 20

     

  • The element must be a node, but the node is not necessarily an element.
3. XML parsing example using dom
  • Example
    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 dom02 {public static void printnodeattr (node) {namednodemap = node. getattributes (); For (INT I = 0; I <namednodemap. getlength (); I ++) {// traverses the attribute node attrnode = namednodemap. item (I); system. out. println (attrnode. getnodename () + ":" + attrnode. getfirstchild (). getnodevalue () ;}} public static void main (string [] ARGs) {documentbuilderfactory factory = documentbuilderfactory. newinstance (); // obtain the factory instance try {documentbuilder builder = factory. newdocumentbuilder (); // Parse XML document DOC = builder through builder. parse ("src/students. XML "); // obtain the document nodelist = Doc. getelementsbytagname ("Students"); // obtain the node element = (element) nodelist by Tag name. item (0); nodelist studentsnodelist = element. getelementsbytagname ("student"); // obtain the node for (INT I = 0; I <studentsnodelist. getlength (); I ++) {Element E = (element) studentsnodelist. item (I); // element is a subclass of node. It has more functions than node. out. println ("name:" + E. getelementsbytagname ("name "). item (0 ). getfirstchild (). getnodevalue (); printnodeattr (E. getelementsbytagname ("name "). item (0); system. out. println ("Gender:" + E. getelementsbytagname ("sex "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("Age:" + E. getelementsbytagname ("Age "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("====================") ;}} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}

     

 

XML-dom parsing XML

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.