[This post was last edited by eclipse at pm,] Use JDOM to process XML documents (reproduced) Keywords: Java, JDOM, XML, jaxb (1) Introduction of JDOM 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 JDOM or jaxb is required. At this year's (2002) javaone conference, Jason Hunter, the main founder of JDOM, gave a wonderful speech about JDOM, entitled JDOM makes XML easy. In that document, JDOM was compared with Dom, and I prefer to compare it with jaxb. Jaxb and JDOM are both developed to provide more convenient XML processing interfaces than Dom and sax in Java, and solve this problem through completely different approaches. JDOM is a tree operation similar to Dom. Jaxb generates Java code to access the XML document through the DTD and binding mode, and maps the XML into a Java object for operation. You can decide which one to use based on your project needs and personal preferences. Compared with jaxb, JDOM has the following characteristics: 1) JDOM is easier to use than jaxb. To use jaxb, you must first write the DTD and then write the binding mode. JDOM does not have such requirements. If you use Java and XML, you can even use JDOM simply by reading JDOM javadoc documents. 2) After compiling the DTD and binding mode in jaxb, the XML document is mapped to a Java object, and its data is the attribute of the Java object, and the data type is converted. Therefore, accessing XML documents is easier than JDOM. 3) The code generated by a DTD and binding mode in jaxb can only access the document restricted by this DTD. If you want to access other XML documents, you need to write a DTD and the binding mode. JDOM can process any XML document, including constrained and unrestricted. Currently, neither JDOM nor jaxb has an official version. The latest version of JDOM is beta8, and jaxb is 1.0 early access. Its standard version is 0.21. Relatively speaking, JDOM is more mature. For example, jaxb does not support namespace and cannot write processing commands to XML documents. Sometimes the linefeeds and spaces we need to retain are automatically filtered out in jaxb, even placed in <! [CDATA [and]> cannot be spared. JDOM does not have these restrictions. If the above three points are determined by the characteristics of JDOM and jaxb and cannot be changed, it indicates that jaxb still needs more work. (2) obtain and install JDOM In the http://jdom.org, you can download the latest version of JDOM. Take JDOM beta8 as an example. After downloading and decompressing, The jdom jar file is the JDOM. jar file under the build Directory, which is added to the class path. In addition, JDOM also requires support for jar files such as xerces. jar in the lib directory. If the following error occurs during use: Java. Lang. nosuchmethoderror Or Java. Lang. noclassdeffounderror: ORG/XML/sax/saxnotrecognizedexception You need to ensure xerces. jar files are located in other XML classes such as JAXP or crimson in classpath. These class files, including older versions of xerces, may not support sax2.0 or DOM Level 2. As a result, the above error occurs. (3) A simple example The JDOM processing method is similar to Dom, but it is mainly implemented using sax, so you don't have to worry about processing speed and memory. In addition, there are almost no interfaces in JDOM, and all the classes are real classes without class factory classes. The most important package org. JDOM mainly has the following classes: ? Attribute ? CDATA ? Comment ? Doctype ? Document ? Element ? Entityref ? Namespace ? Processinginstruction ? Text The XML document used for data input must use the org. JDOM. Input package, which in turn requires org. JDOM. output. As mentioned above, you can use it by reading the API documentation. In our example, we read the XML file examplea. XML, add a Processing Instruction, modify the price and author of the first book, add an attribute, and write the file exampleb. xml: // Examplea. xml <? XML version = "1.0" encoding = "GBK"?> <Booklist> <Book> <Name> JAVA Programming basics </Name> <Author> Zhang San </author> <Publishdate> 2002-6-6 </publishdate> <Price> 35.0 </price> </Book> <Book> <Name> application of XML in Java </Name> <Author> Li Si </author> <Publishdate> 2002-9-16 </publishdate> <Price> 92.0 </price> </Book> </Booklist> --------------------------------------------------- Import org. JDOM .*; Import org. JDOM. Input .*; Import org. JDOM. Output .*; Import java. Io .*; Public class cute { Public static void main (string ARGs []) { Try { Org. JDOM. Input. saxbuilder sb = new org. JDOM. Input. saxbuilder (); // Create a document Org. JDOM. Document Doc = sb. Build (New fileinputstream ("C: // example. xml "); // Add a Processing Instruction Org. JDOM. processinginstruction Pi = new processinginstruction ("XML-stylesheet", "href =/" booklist.html. XSL/"type =/" text/XSL /""; // Add this processing instruction to the document Doc. addcontent (PI ); // Obtain the elements of this document Org. JDOM. Element El = Doc. getrootelement (); // Obtain all child elements of the element Java. util. List ls = El. getchildren (); // Obtain the first child element Org. JDOM. Element book = (element) ls. Get (0 ); // Add an attribute to the word element Org. JDOM. Attribute ATTR = new attribute ("hot", "true "; Book. setattribute (ATTR ); // Obtain the child element of this element (specified) Org. JDOM. Element EL2 = book. getchild ("author "; // Output the value of this element System. Out. println (el2.getname ()); // Rename the value of this element El2.settext ("cute "; // Specify the element to obtain the value. Org. JDOM. Element EL3 = book. getchild ("price "; // Change the value El3.settext (float. tostring (50366f )); String A = ""; Boolean bool = true; Org. JDOM. Output. xmloutputter xml = new org. JDOM. Output. xmloutputter (A, bool, "gb2312 "; XML. Output (Doc, new fileoutputstream ("C: // cute. xml "); } Catch (exception e ){ System. Out. println (E. getmessage ()); } } } |