Java EE XML dom4j parsing

Source: Internet
Author: User

dom4j parsing

@author Ixenos

dom4j Common APIs

Saxreader reader = new saxreader();

The Document object is also considered a root node

Document doc = reader. Read (file file);

Iterate sub-nodes under current node/can also be foreach

Iterator ITR = doc.nodeiterator();

while (Itr.hasnext ()) {node node = Itr.next ();}

Get root node

Element Rootelem = doc.getrootelement();

Gets the first sub-label of the specified name under the current label

Element Contactelem = Rootelem. element ("Contact");

Get all the sub-tags under the current tab

list<element> list = Rootelem. Elements ();

Get deeper labels (methods can only be obtained at a layer level),XPath!!! Of course, this only gets the first one.

Element Nameelem = Doc.getrootelement (). element ("Contact"). element ("name");

Gets the property value of the specified name

String idvalue = Contactelem. AttributeValue ("id");

Gets the Property object for the specified property name

Attribute idattr = Contactelem. attribute ("id");

Get all Property objects, return to list collection

list<attribute> list = Contactelem. attributes ();

Get all Property objects, return iterators

iterator<attribute> it = Contactelem. Attributeiterator ();

while (It.hasnext ()) {Attribute attr = It.next ();}

Gets the text content of the current tag, noting that the space and line wrapping are also text

String content = Doc.getrootelement (). GetText ();

Get the text (get the label first, then get the text on the label)

Element Nameelem =doc.getrootelement (). Element ("contact"). Element ("name");

And then get the text

String text = Nameelem.gettext ();

System.out.println (text);

Directly get the text content of the specified sub-label name, directly get the phone tag text

String Text2 = Doc.getrootelement (). Element ("contact"). Elementtext ("Phone");

1, the most basic: Document object refers to the entire XML file

Reads an XML document and returns the file object

Saxreader reader = new saxreader ();

Reads an XML document and returns the file object

Document doc = Reader.read (new File ("03.contact.xml"));

2, the Document object's Nodeiterator method iterates the next level, determines whether is the element object, next level again uses the Nodeiterator iteration next level, the double circulation, or uses the recursion, can traverse all label nodes

Nodeiterator: Gets all child node objects under the current label node

Iterator<node> it = Doc.nodeiterator ();

while (It.hasnext ()) {

Node node = It.next ();

String name = Node.getname ();

System. out. println (name);

Continue to remove the following child nodes

Only the label node has child nodes

So judging whether the current label is a label node

if (Node instanceof Element) {

Element elem = (element) node;

Iterator<node> it2 = Elem.nodeiterator ();

while (It2.hasnext ()) {

Node Node2 = It2.next ();

System. out. println (Node2.getname ());

}

}

}

DOM4J full Read XML content example

1 ImportJava.io.File;2 ImportJava.util.Iterator;3 Importjava.util.List;4 5 ImportOrg.dom4j.Attribute;6 Importorg.dom4j.Document;7 Importorg.dom4j.Element;8 ImportOrg.dom4j.Node;9 ImportOrg.dom4j.Text;Ten ImportOrg.dom4j.io.SAXReader; One Importorg.junit.Test; A /** - * Practice-Full read of XML document content -  * @authorAPPle the  * -  */ -  Public classDemo3 { -  + @Test -      Public voidTest ()throwsexception{ +         //reading an XML document ASaxreader reader =NewSaxreader (); atDocument doc = -Reader.read (NewFile ("./src/contact.xml"))); -  -         //Read root tag -Element Rootelem =doc.getrootelement (); -  inStringBuffer SB =NewStringBuffer (); -  to getchildnodes (ROOTELEM,SB); +  - System.out.println (sb.tostring ()); the  *     } $ Panax Notoginseng     /** - * Gets all the child tags of the current label the      */ +     Private voidgetchildnodes (Element elem,stringbuffer sb) { A         //System.out.println (Elem.getname ()); the  +         //start Tag -Sb.append ("<" +elem.getname ()); $  $         //get a list of attributes for a label -         //Why is the assembly of attributes placed outside the loop body? Since the loop body is the child node of the element root node, it is not possible to assemble the properties of the root tag, but only leave it to the following recursive mates to iterate Citrix curve to salvation -List<attribute> attrs =elem.attributes (); the         if(attrs!=NULL){ -              for(Attribute attr:attrs) {Wuyi                 //System.out.println (Attr.getname () + "=" +attr.getvalue ()); the                 //attribute value double quotation mark using escape character -Sb.append ("" +attr.getname () + "=\" "+attr.getvalue () +" \ ""); Wu             } -         } AboutSb.append (">"); $  -         //Get text -         //String content = Elem.gettext (); -         //System.out.println (content); AIterator<node> it =elem.nodeiterator (); +          while(It.hasnext ()) { theNode node =It.next (); -  $             //label the             if(nodeinstanceofElement) { theElement el =(Element) node; the getchildnodes (EL,SB); the             } -  in             //text the             if(nodeinstanceofText) { theText Text =(Text) node; About Sb.append (Text.gettext ()); the             } the         } the  +         //end Tag -Sb.append ("</" +elem.getname () + ">"); the Bayi  the  the  -     } -}

Java EE XML dom4j parsing

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.