Treat XML document with JDOM

zhaozj2021-02-17  44

(1) JDM 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. The comparison of JDOM and JAXB is from its own characteristics: 1) JDOM is easier to get in more 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.

(2) Get and install the JDOM at http://jdom.org to download the latest version of JDOM. 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 you appear in use: java.lang.nosuchmethoderror or java.lang.noclassdeffounderror: 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 classes Files, including the previous versions of Xerces, may not support SAX2.0 or DOM Level 2. The above errors have so.

(3) A simple example of JDOM has some 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? Processinginstruction? Text data Enter to use the XML document to pass org.jdom.input package In turn, 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, add an attribute, then write file eXampleb.xml: //examplea.xml Java programming Zhang 3 2002-6-6 35.0 XML in Java Application Li 4 2002-9-16 92.0 < / Price> // 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 there is no need for document doc = sb.build (New fileInputStream ("eXamplea.xml")); // Add a handling instruction Processinginstruction PI = New ProcessingInstruction ("XML-Stylesheet", "HREF = /" BookList.html.xsl / "Type = /" text / xsl / "); Doc.AddContent (PI);

Element root = doc.getrootelement (); // Get root element java.util.list books = root.getchildren (); // Get a collection of root elements all child elements ELEMENT book = (Element) books.get (0); // Get the first BOOK element / / Add a property to the first book Attribute A = New Attribute ("Hot", "True"); book.setttribute (a); element author = book.getchild ("author" ); // Get the specified word elements 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, more depressed is that we must convert the data type itself, and this is the advantage of Jaxb Author.Settext (float.tostring (50.0 f)); string indent = ""; boolean newline = true; xmloutputter outp = new XMLOUTPUTTER (INDENT, NewLines, "GBK"); Outp.output (DOC, New FileoutPutStream ("Exampleb.xml");

}};

Execute EXAMPLEB.XML: Java programming 50.0 2002-6-6 35.0 XML in Java in Java < PublishDate> 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 ().

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

New Post(0)