Parsing XML in the Java Dom Way

Source: Internet
Author: User
Tags tag name

Tagged with: att val class dbf path builder Dev nodelist Tor

1 //Create a documentbuilderfactory factory instance2Documentbuilderfactory dbf=documentbuilderfactory.newinstance ();3         //creating a Documentbuilder instance from a factory instance object4Documentbuilder db=Dbf.newdocumentbuilder ();5         //parses the URL specified by the XML file and saves it to the D variable6Document d=db.parse ("url path");7         //get node labels by tag name8NodeList nl=d.getelementsbytagname ("label");9         //looping through the NL collectionTen          for(intI=0;i<nl.getlength (); i++){ One             //get the specified node nodes by index value ANode n=Nl.item (i); -             /*Gets the specified property (known property name and number 1) - * node coercion type conversion the * Element e= (Element) E; - * Get property values by property name - * E.getattribute ("Node_name"); -              * */ +             //get all the properties of a node -NamedNodeMap nnm=n.getattributes (); +             //Traverse attribute Map Collection A              for(intJ=0;j<nnm.getlength (); j + +){ at                 //get the specified property by index -Node attributenode=Nnm.item (j); -                 //get attribute node name name - attributenode.getnodename (); -                 //Get Property Node Value property - Attributenode.getnodevalue (); in             } -              to             //get all child nodes of n node +NodeList nl1=n.getchildnodes (); -             //traversing the NL1 collection the              for(intK=0;k<nl1.getlength (); k++){ *                 //filter out the non-node $                 if(Nl1.item (k). Getnodetype () = =Node.element_node) {Panax Notoginseng                     //Get node name - Nl1.item (k). Getnodename (); the                     //gets the value of the child node of the current node, and the XML file is all nodes +                     //You can also get a text node by using the Nl1.item (k). Gettextcontent () method A                     //The gettextcontent difference is that the text value of the element that will handle the node is combined to output the Nl1.item (k). Getfirstchild (). Getnodevalue (); +                 } -             } $}

Parsing XML in the Java Dom Way

Related Article

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.