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