First look at a section of dom4j code import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.io.XMLWriter;
Import java.io. * ;
public class Xmldom4j {
public void Generatedocument () {
Create a document instance using the Documenthelper class. Documenthelper is the dom4j API factory class that generates XML document nodes
Document document = Documenthelper.createdocument ();
Use the AddElement () method to create the root element catalog. AddElement () is used to add elements to an XML document
Element catalogelement = document.addelement ("catalog");
Add a comment "an XML catalog" using the AddComment () method in the Catalog element
Catalogelement.addcomment ("an XML Catalog");
Add a processing instruction using the Addprocessinginstruction () method in the Catalog element
Catalogelement.addprocessinginstruction ("Target", "text");
Adding journal elements using the AddElement () method in the Catalog element
Element journalelement = catalogelement.addelement ("journal");
Add title and Publisher properties to the journal element using the AddAttribute () method
Journalelement.addattribute ("title", "XML Zone");
Journalelement.addattribute ("publisher", "IBM developerWorks");
Adding journal elements to the article element
Element articleelement = journalelement.addelement ("article");
Increase the level and date attributes for the article element
Articleelement.addattribute ("level", "intermediate");
Articleelement.addattribute ("date", "December-2001");
Add the title element to the article element
Element titleelement = articleelement.addelement ("title");
Use the SetText () method to set the text of the article element
Titleelement.settext ("Java configuration with XML Schema");
Adding author elements to the article element
Element authorelement = articleelement.addelement ("author");
Add the FirstName element to the author element and set the text of the element
Element firstnameelement = authorelement.addelement ("FirstName");
Firstnameelement.settext ("Marcello");
Add the LastName element to the author element and set the text of the element
Element lastnameelement = authorelement.addelement ("LastName");
Lastnameelement.settext ("Vitaletti");
You can use the Adddoctype () method to add document type descriptions
Document.adddoctype ("Catalog", NULL, "FILE://C:/DTDS/CATALOG.DTD");
try {
File File = new file ("C:/catalog/catalog.xml");
File dir = new file ("c:/catalog/");
if (File.isdirectory () | |! dir.exists ()) {
Dir.mkdirs ();
File.createnewfile ();
}
System.out.println (dir + "___" + file);
XMLWriter output = new XMLWriter (new FileWriter (file));
Output.write (document);
Output.close ();
catch (IOException e) {
System.out.println (E.getmessage ());
}
}
public static void Main (string[] argv) {
xmldom4j dom4j = new xmldom4j ();
Dom4j.generatedocument ();
}
DOM4J is a open source XML parsing package produced by dom4j.org, which is defined in its Web site: dom4j is a easy to use, open Source library for working with XML, XPath and XSLT on the Java platform using the "Java Collections Framework" and "Support for DOM, SAX and JAXP."
dom4j
is an easy-to-use, open Source Library for
XML
,
XPath
and
XSLT
. It is applied to
Java
platform, using a
Java
Collection Framework and fully supports
DOM
,
SAX
and
JAXP
. DOM4J is very simple to use. As long as you understand the basic Xml-dom model, you can use it. However, his own guide is only a short page (HTML), but it is quite full. There are few Chinese materials in China. So I write this short tutorial convenient for everyone to use, this article only talks about the basic usage, if need to use deeply, please ... Explore or find other information on your own. Read the IBM Developer Community article (see appendix) and mention the performance comparisons of some XML parsing packages, where dom4j performance is excellent and ranked among several tests. (In fact, this comparison is also referenced in DOM4J's official documentation) so I used dom4j as an XML parsing tool for this project. In the domestic more popular is the use of jdom as a parser, both good their length, but dom4j the biggest feature is the use of a large number of interfaces, which is considered more flexible than the main reason for Jdom. Didn't the master say, "interface-oriented programming". More and more dom4j are being used. If you are good at using Jdom, you might as well continue to use it, just look at this article as an understanding and comparison, if you are going to adopt a parser, it is better to use dom4j bar. Its main interfaces are in Org. DOM4J is defined in this package:
| Attribute |
attribute defines the attributes of XML |
| Branch |
Branch defines a public behavior for nodes that can contain child nodes such as XML elements (element) and documents (docuemnts). |
| CDATA |
CDATA Defines an XML CDATA region |
| Characterdata |
Characterdata is an identification excuse for identifying character-based nodes. such as Cdata,comment, Text. |
| Comment |
Comment defines the behavior of XML annotations |
| Document |
The XML document is defined |
| DocumentType |
DocumentType define XML DOCTYPE declaration |
| Element |
element defines XML elements |
| Elementhandler |
Elementhandler defines the processor for the Element object |
| ElementPath |
Used by Elementhandler to get the path-level information currently being processed |
| Entity |
Entity defines XML entity |
| Node |
Node defines the polymorphic behavior for all XML nodes in the DOM4J |
| Nodefilter |
Nodefilter defines the behavior of a filter or predicate that is produced in a dom4j node (predicate) |
| ProcessingInstruction |
ProcessingInstruction defines an XML processing instruction. |
| Text |
Text defines an XML text node. |
| Visitor |
Visitor is used to implement Visitor mode. |
| XPath |
XPath provides an XPath expression after parsing a string |
The name is roughly what it means to see it. To understand this set of interfaces, the key is to understand the interface of the inheritance relationship: interface Java.lang.
cloneableInterface org.dom4j.
NodeInterface org.dom4j.
AttributeInterface org.dom4j.
BranchInterface org.dom4j.
DocumentInterface org.dom4j.
ElementInterface org.dom4j.
CharacterdataInterface org.dom4j.
CDATAInterface org.dom4j.
CommentInterface org.dom4j.
TextInterface org.dom4j.
DocumentTypeInterface org.dom4j.
EntityInterface org.dom4j.
ProcessingInstructionAt a glance, a lot of things are clear. Most of them are inherited by node. Knowing these relationships, the future of writing programs will not appear classcastexception. Here are some examples (some of which are excerpted from DOM4J's own documentation) and simply say how to use them. 1. Reading and parsing XML documents: Reading and writing XML documents are mainly dependent on org. Dom4j.io packages, which provide domreader and saxreader in two different ways, and are called the same way. This is the benefit of relying on interfaces.
| /read XML from file, enter filename, return XML document public Document Read (String fileName) throws Malformedurlexception, documentexception { & nbsp; |