Treat XML document with JDOM

zhaozj2021-02-11  225

Welcome to discuss me:

Boyofjava@sina.com

JDOM introduction and comparison with JAXB

Java XML = JDOM!

This is the goal of the JDOM designer. If you have used annoying SAX or DOM to process XML, you will know why there is JDOM or Jaxb. The main founder Jason Hunter of JDOM in Javaone this year (2002) has a wonderful speech introduces Jdom Technology, the topic is Jdom Makes XML Easy.

In that document, Jdom is compared to DOM, and I am more willing to take it with Jaxb. Because JAXB and JDOM are developed in Java to provide a more convenient XML handling interface than the DOM and SAX, and solve this problem by a completely different way. JDOM processing is a tree operand similar to DOM. JAXB generates Java code accessed through DTD and binding mode, map XML to Java objects. You can decide which one is used according to the needs of the project and personal preferences.

JDOM and JAXB comparison, from itself, look:

1) JDOM is easier to play than JAXB. First use JAXB first, you will write DTD, and then write a binding mode. JDOM does not require this, if you will java and XML, you can even say that you can use JDOM's Javadoc documentation.

2) After the JAXB is written in DTD and binding mode, the XML document is mapped to a Java object. Its data is the properties of the Java object, and even the data type has been converted. Therefore, accessing the XML document is simpler than JDOM, you can say It is always for all.

3) JAXB generated by a DTD and binding mode can only access the document constrained by the DTD. If you want to access another XML document, you need to write DTD and binding mode. JDOM can handle any XML documents, including constraints and unconstrained.

There is currently no official version in Jdom and JAXB. JDOM's latest version is Beta8, Jaxb is 1.0 Early Access, and its specification is 0.21. Relatively speaking, JDOM is more mature. For example, JAXB does not support the namespace, and cannot write to the XML document. Sometimes we need to retain the wrapper and the first space to automatically filter it in JAXB, and then put it in Flexible. JDOM has no limitations. If the above 3 points is determined by the characteristics of Jdom and Jaxb itself, it is almost impossible to change, then this shows that JAXB also needs more work.

Get and install JDOM

You can download the latest version of JDOM at http://jdom.org. Take the 2 credit version of JDOM Beta8 as an example. After downloading, decompress the JAR file of JDOM is the file jdom.jar in the build directory, and will join the classpath. In addition, JDOM also requires support of those JAR files such as Xerces.jar in the lib directory. If the following error occurs in use:

Java.lang.nosuchmethodError

or

Java.lang.noclassdefounderror: ORG / XML / SAX / SAXNOTRECognizedException

You need to ensure that Xerces.jar files are located in other XML classes in ClassPath, such as JAXP or CRIMSON, these class files, including previous versions of Xerces, may not support SAX2.0 or DOM Level 2. The above errors have so.

A simple example

JDOM's processing is somewhat similar to DOM, but it is mainly implemented with SAX, you don't have to worry about processing speed and memory. In addition, there is almost no interface in JDOM, all of which are real class, and there is no class. Its most important package Org.JDom has the following classes: - Attribute

- CDATA

- Comment

- DOCTYPE

- Document

- ELEMENT

- Entityref

- Namespace

- ProcessinginstructionTRuction

- Text

Data input To use the XML document to pass the org.jdom.input package, it needs org.jdom.Output. As mentioned earlier, the closure is to use the API documentation.

Our example reads XML files EXAMPLEA.XML, add a handling instruction, modify the price of the first book and the author, and add a property, then write to the file eXampleb.xml:

//examplea.xml

Java programming

Zhang 3

2002-6-6

35.0

XML in Java

Li 4

2002-9-16

92.0

//testjdom.java

Import Org.jdom. *;

Import Org.jdom.Output. *;

Import org.jdom.Input. *;

Import java.io. *;

Public class testjdom {

Public static void main (string args []) throws exception {

SAXBUILDER SB = New Saxbuilder ();

// Construct a document from the file because the encoding has been specified in the XML file, so it is not necessary.

Document Doc = Sb. Build (New FileInputStream ("EXAMPLEA.XML"));

/ / Add a processing instruction

Processinginstruction PI = New ProcessinginstructionTRuction

("XML-Stylesheet", "HREF = /" BookList.html.xsl / "Type = /" text / xsl / ");

Doc.addContent (pi);

Element root = doc.getrootElement (); // Get root elements

Java.util.list books = root.getchildren (); // Get a collection of all child elements

Element book = (element) books.get (0); // Get the first BOOK element

/ / Add a property for the first book

Attribute A = New Attribute ("Hot", "True"); book.setttribute (a);

ELEMENT Author = BOOK.GETITCHILD ("Author"); // Get the specified word element

Author.settext ("Wang 5"); // Change the author to Wang 5

// or text t = new text ("Wang 5"); book.addcontent (t);

ELEMENT price = book.getchild ("price"); // Get the specified word element

/ / Modify the price, which is relatively depressed is that we must convert data types themselves, and this is the advantage of JAXB

Author.Settext (Float.Tostring (50.0F));

String indent = ""

Boolean newlines = true;

XMloutPutter Outp = New XMLOUTPUTTER (INDENT, Newlines, "GBK");

Outp.output (DOC, New FileoutputStream ("Exampleb.xml");

}

}

Executive result EXAMPLEB.XML:

Java programming

50.0

2002-6-6

35.0

XML in Java

Li 4

2002-9-16

92.0

By default, the method of getText () of the Element class of JDOM does not filter the blank character if you need to filter, with setTextTrim ().

Reference documentation

1) JDOM Makes XML Easy (http://www.servlets.com/speaking/jdom-javaone.pdf)

2) The JavaTM Architecture for XML Binding User's Guide (http://java.sun.com/xml/jaxb/jaxb-docs.pdf)

3) Web Services Made Easier. The Java TM Apis and Architectures for XML, A Technical White Paper (http://java.sun.com/xml/webservices.pdf)

转载请注明原文地址:https://www.9cbs.com/read-5151.html

New Post(0)