DOM4 J Use basic Guidelines (reprint) __ Documents

Source: Internet
Author: User
Tags access properties
dom4j is a simple, flexible, open source library that supports XML, XPath, and XSLT. Like Jdom, DOM4J also applies to the Java platform. The DOM4J API uses the Java Collection Framework and fully supports DOM, sax, and Jaxp.
DOM4J was isolated and developed by people who developed jdom early on. Unlike Jdom, DOM4J uses interfaces and abstract base classes, and although the DOM4J API is relatively complex, it provides greater flexibility than jdom.

Dom4j is a very, very good Java XML API with excellent performance, powerful features and extreme ease of use, and it is also an open-source software.

DOM4J API Introduction

The dom4j-1.6.1 API has 14 packages, where the ORG.DOM4J package includes an interface that represents the components of an XML document, Org.dom4j.io includes classes that build dom4j document trees, and classes that output dom4j document trees.

(1) Create an XML document
A Documenthelper class is provided in the Org.dom package, and many of the methods in the class are static methods, including methods for creating the various components of the XML document.

Document doc=documenthelper.createdocument ();
Element eltroot=documenthelper.createelement_x ("student");
Doc.setrootelement (Eltroot);

You can also create the element object first, and then create the root element while creating the document object, as follows:

Element eltroot=documenthelper.createelement_x ("student");
Document doc=documenthelper.createdocument (eltroot);

(2) Adding attributes
In dom4j, adding attributes is as simple as Jdom, as follows:

Eltroot.addattribute ("SN", "01");

(3) Adding child elements and setting element contents
In the branch interface, the AddElement () method is defined, as follows:

Public Element addelement (String name)

This method adds a child element node to the current node with the specified name and returns a reference to the new node. We use this method to add child elements to the root element, as follows:

Element eltname=eltroot.addelement ("name");
Element eltage=eltroot.addelement ("Age");
Eltname.settext ("Awang");
Eltage.settext ("22");

(4) Output document
DOM4J provides a variety of output forms for XML documents. In the Org.dom4j.io package, the Domwriter class can output the dom4j tree as the document object of the Consortium DOM, and the Saxwriter class can output the dom4j tree as a sax event stream. The XmlWriter class provides the ability to output parts of the dom4j tree in an XML stream.
For example, the following code prints the XML document you created to the screen:

XMLWriter xmlwriter=new XMLWriter ();
Xmlwriter.write (DOC);

You can also choose to output to the file as follows:

XMLWriter xmlwriter=new XMLWriter (New Java.io.FileWriter ("Student.xml"));
Xmlwriter.write (DOC);
Xmlwriter.close ();

DOM4J also provides a class Org.dom4j.io.OutputFormat that controls the format of document output. This class allows you to set the character encoding of the output document, set the line delimiter, indent the string, and so on.
The following code sets the output format to use 4 spaces as indented strings, adding new rows between elements.

OutputFormat outfmt=new OutputFormat ("", true);
XMLWriter xmlwriter=new XMLWriter (OUTFMT);
Xmlwriter.write (DOC);

The following code prints the document using a glorified format, sets the character encoding to GB2312, and uses 4 spaces as indents.

OutputFormat Outfmt=outputformat.createprettyprint ();
Outfmt.setencoding ("GB2312");
Outfmt.setindent ("");
XMLWriter xmlwriter=new XMLWriter (OUTFMT);
Xmlwriter.write (DOC);

(5) Constructing dom4j tree
In the Org.dom4j.io package, the DOM4J provides two classes: Domreader and Saxreader,domreader build dom4j trees from an existing one, while Saxreader uses the SAX parser, Build the dom4j tree from different input sources.
The code to build the Dom4j Document object with the Saxreader class is as follows:

Saxreader saxreader=new Saxreader ();
File File=new file ("Student.xml");
Document doc=saxreader.read (file);

(6) Accessing the root element
In the document class, the Getrootelement () method is given to return the root element of the document. As follows:

Element Root=doc. RootElement ();

(7) Access node
To get all the child elements of an element, as follows:

Java.util.List childrenlist=elt.elements ();

To get all the child elements of an element with a given name, as follows:

Java.util.List childrenlist=elt.elements ("student");

To get the first word element with a given name for an element, as follows:

Element eltchild=elt.element ("student");

To iterate through all the child elements of an element, as follows:

For (Java.util.Iterator it=root.elementiterator (); It.hasnext ();)
{
Element element= (Element) It.next ();
}

To delete an element, you can use the Remove () method defined in the branch interface as follows:

Element eltstu=root.element ("student");
Root.remove (Eltstu);

(8) Access properties
To get all the attributes of an element, as follows:

Java.util.List sttrlist=elt.attributes ();

To get the specified property, the following:

Attribute Attr=elt.attribute ("SN");

To get the value of an attribute, as follows:

String attrvalue=elt.attributevalue ("SN");

To delete a property, as follows: Elt.remove (Elt.attribute ("SN"));

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.