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:
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.
(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:
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.