First, in the form of code. Get node: IteratorElement. nodeIterator (); get all the subnodes under the current label node first, and then give it in code form.
Get nodes:
Iterator Element. nodeIterator (); // Obtain all subnodes under the current tag node
Obtain tags:
Element Document. getRootElement (); // get the root tag of the xml Document
Element ELement. element ("tag name") // The first sub-tag of the specified name
Iterator Element. elementIterator ("tag name"); // All child tags with the specified name
List Element. elements (); // Obtain all child tags
Get attributes:
String Element. attributeValue ("attribute name") // Obtain the attribute value of the specified name
Attribute Element. attribute ("Attribute name"); // gets the attribute object of the specified name
Attribute. getName () // Get the Attribute name
Attivity. getValue () // get the property value
List Element. attributes (); // Obtain all attribute objects. recommended solution.
Iterator Element. attibuteIterator (); // Obtain all attribute objects
Get text:
Element. getText (); // get the text of the current tag
Element. elementText ("tag name") // Obtain the text content of the child tag with the specified name of the current tag
Case:
Import java. io. file; import java. util. iterator; import java. util. list; import org. dom4j. attribute; import org. dom4j. document; import org. dom4j. element; import org. dom4j. node; import org. dom4j. io. SAXReader; import org. junit. test; /*** second dom4j reads xml file content * node * tag * attribute * text * @ author APPle **/public class Demo2 {/*** get node information */@ testpublic void test1 () throws Exception {// 1. read the xml Document and return the Document object SAXReader reader = new SAXReader (); Document doc = reader. read (new File (". /src/contact. xml "); // 2. nodeIterator: Get all the subnode objects under the current node ([do not contain nodes below Sun]) Iterator
It = doc. nodeIterator (); // The Returned result is an iterator object. the iterator stores Node and provides the generic Nodewhile (it. hasNext () {// determine whether there is any next element Node = it. next (); // Retrieve the element String name = node. getName (); // Get the node name // System. out. println (name); // System. out. println (node. getClass (); // Continue to retrieve the subnodes under it. // only the [label node] has subnodes. // Determine whether the current node is a label node. if (node instanceof Element) {Element elem = (Element) node; // node is of the Node type, so the Iterator should be transformed down.
It2 = elem. nodeIterator (); while (it2.hasNext () {Node n2 = it2.next (); System. out. println (n2.getName () ;}}}/*** traverse all nodes in the xml document * @ throws Exception */@ Testpublic void test2 () throws Exception {// 1. read the xml Document and return the Document object SAXReader reader = new SAXReader (); Document doc = reader. read (new File (". /src/contact. xml "); // get the root tag. ContactListElement rooElem = doc. getRootElement (); getChildNodes (rooElem);}/*** get all subnodes under the passed tag. Element elem only the Element label has a subnode * @ param elem */private void getChildNodes (Element elem) {System. out. println (elem. getName (); // get the root tag name // Get the subnode Iterator
It = elem. nodeIterator (); while (it. hasNext () {Node node = it. next (); // 1. determine whether the label node is if (node instanceof Element) {Element el = (Element) node; // recursive getChildNodes (el );}};} /*** get tag */@ Testpublic void test3 () throws Exception {// 1. read the xml Document and return the Document object SAXReader reader = new SAXReader (); Document doc = reader. read (new File (". /src/contact. xml "); // 2. obtain the root tag Element rootElem = doc. getRootElement (); // Obtain the tag name String name = rootElem. getName (); System. out. println (name); // contactList // 3. obtain the [first] [sub] tag/* Element contactElem = rootElem for the specified name under the current tag. element ("contact"); System. out. println (contactElem. getName (); * // 4. obtain all the sub-tags with the specified name under the current tag. if the contact name is specified, all the tags with the same name will be obtained. // The name is specified here, with restrictions/* Iterator
It = rootElem. elementIterator ("contact"); while (it. hasNext () {Element elem = it. next (); System. out. println (elem. getName ();} * // 5. obtain all the sub-tag objects under the current rootElem tag. all sub-tag objects are displayed here. Recommended. List
List = rootElem. elements (); // method for traversing the List // 1) Traditional for loop 2) enhanced for loop 3) iterator/* for (int I = 0; I
It = list. iterator (); // ctrl + 2 loosen lwhile (it. hasNext () {Element elem = it. next (); System. out. println (elem. getName ();} * // obtain deeper tags (methods can only be obtained layer by layer) Element nameElem = doc. getRootElement (). element ("contact "). element ("name"); System. out. println (nameElem. getName ();}/*** get attribute */@ Testpublic void test4 () throws Exception {// 1. read the xml Document and return the Document object SAXReader reader = new SAXReader (); Document doc = reader. read (new File (". /src/contact. xml "); // Get attributes: (obtain the tag object of the attribute before obtaining the attribute) // 1. obtain the tag object Element contactElem = doc. getRootElement (). element ("contact"); // 2. get the property // 2.1 get the property value of the specified name/* String idValue = contactElem. attributeValue ("id"); System. out. println (idValue); * // 2.2 get the Attribute object Attribute/* Attribute idAttr = contactElem of the specified Attribute name. attribute ("id"); // getName: property name getValue: property value System. out. println (idAttr. getName () + "=" + idAttr. getValue () ); * // 2.3 get the [all] attribute object and return the LIst set. You do not need to know the attribute name and how many attributes/* List list = contactElem. attributes (); // recommended solution // traverse attributes for (Attribute attr: list) {System. out. println (attr. getName () + "=" + attr. getValue ();} * // 2.4 get the [all attributes] object and return the Iterator it = contactElem. attributeIterator (); while (it. hasNext () {Attribute attr = it. next (); System. out. println (attr. getName () + "=" + attr. getValue () ;}}/*** get text */@ Testpublic void test5 () throws Exception {// 1. read the xml Document and return the Document object SAXReader reader = new SAXReader (); Document doc = reader. read (new File (". /src/contact. xml ");/*** NOTE: spaces and line breaks are also xml content */String content = doc. getRootElement (). getText (); System. out. println (content); // get the text (first obtain the tag object, then obtain the text on the tag) Element nameELem = doc. getRootElement (). element ("contact "). element ("name"); // 1. obtain the text String text = nameELem. getText (); System. out. println (text); // 2. string text2 = doc. getRootElement (). element ("contact "). elementText ("phone"); System. out. println (text2 );}}
The above is the content of the xml file read by java Domj4. For more information, see PHP Chinese network (www.php1.cn )!