JDOM is only a Java XML Parser suitable for Java programmers. Java + xml = JDOM!
The main founder of JDOM (like two founders) Jason hunter gave a wonderful speech about JDOM, entitled JDOM makes XML easy.
In the http://jdom.org, you can download the latest version of JDOM. Take the binary version of JDOM 1.0 as an example. After downloading and decompressing, The jdom jar file is the JDOM. jar file under the build Directory, which is configured to classpath and can be placed in the Java, SQL, and JDOM libraries in sequence. At this time, javac is compiled successfully, but Tomcat is not working yet. In the original Tomcat parsing, jbom is also required. jarcopy to Tomcat's common \ Lib (by the way, let's talk about some debugging experience: After Tomcat adds a new class, you need to restart tomcat to find these class files, another environment variable also needs to be restarted. When classnofound appears, it must be not configured with classpath)
In addition, JDOM also requires support for jar files such as xerces. jar and JAXP. 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.
(No tests are available below)
3. parse XML using JDOM
Now it's the right question. The following is a simple example to illustrate how to use JDOM, a toolkit suitable for Java programmers to parse XML documents.
For simplicity, I used the following XML as the XML file to be parsed:
<? XML version = "1.0" encoding = "gb2312"?>
<Books>
<Book email = "zhoujunhui">
<Name> zhangzwon </Name>
<Price> 60.0 </price>
</Book>
</Books>
It is simple enough, but it has subnodes and attributes that we care about.
The following is a Java file used to parse the XML file:
1 public class jdomparse {
2 Public jdomparse (){
3 string xmlpath = "library. xml ";
4 saxbuilder builder = new saxbuilder (false );
5 try {
6 document DOC = builder. Build (xmlpath );
7 element books = Doc. getrootelement ();
8 list booklist = books. getchildren ("book ");
9 For (iterator iter = Booklist. iterator (); ITER. hasnext ();){
10 element book = (element) ITER. Next ();
11 string email = book. getattributevalue ("email ");
12 system. Out. println (email );
13 string name = book. getchildtexttrim ("name ");
14 system. Out. println (name );
15 book. getchild ("name"). settext ("alter#z.pdf ");
16
17}
18
19 xmloutputter outputter = new xmloutputter ();
20 outputter. Output (Doc, new fileoutputstream (xmlpath ));
21
22} catch (jdomexception e ){
23 E. printstacktrace ();
24} catch (ioexception e ){
25 E. printstacktrace ();
26}
27}
28 public static void main (string [] ARGs ){
29 new jdomparse ();
30}
31}
There are less than 30 lines of code. Now I will explain the Code:
4. Explain the code
Referenced class:
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. util. iterator;
Import java. util. List;
// The following is the class referenced to JDOM
Import org. JDOM. Document;
Import org. JDOM. element;
Import org. JDOM. jdomexception;
Import org. JDOM. Input. saxbuilder;
Import org. JDOM. Output. xmloutputter;
(1) To use JDOM, you must first specify the parser to use. For example:
Saxbuilder builder = new saxbuilder (false); this indicates that the default parser is used.
(2) obtain the document. All operations we will perform in the future will be performed on this document:
Document Doc = builder. Build (xmlpath );
(3) Get the root element:
Element books = Doc. getrootelement ();
All nodes in JDOM (Concepts in Dom) are org. JDOM. element classes. Of course, their subnodes are also org. JDOM. element classes.
(4) obtain a set of elements (nodes:
List booklist = books. getchildren ("book ");
This indicates that the "books" element is named "book" and these elements are put in a list set.
(5) Round Robin list set
For (iterator iter = Booklist. iterator (); ITER. hasnext ();){
Element book = (element) ITER. Next ();
}
Another round robin method is:
For (INT I = 0; I <Booklist. Size (); I ++ ){
Element book = (element) Booklist. Get (I );
}
(6) Get element attributes:
String email = book. getattributevalue ("email ");
Obtain the attribute value of the element book "email.
(7) obtain the value of the child element (the lowest layer element) of the element:
String name = book. getchildtexttrim ("name ");
Note that there must be only one sub-element named "name" of the book element.
(8) change the value of an element (the lowest layer element:
Book. getchild ("name"). settext ("alter#z ");
This is only a modification to the document and is not modified in the actual XML document.
(9) Save the document modification to the XML file:
Xmloutputter outputter = new xmloutputter ();
Outputter. Output (Doc, new fileoutputstream (xmlpath ));
We need to first have an xmloutputter class, and then save the modified document to the XML document.
So far. After using JDOM to parse and process XML documents, it is easy to understand. Now we have a general idea of JDOM.
Summary:
Basic Process:
1. Create a parser
// Saxbuilder sb = new saxbuilder ();
Return document
// Document DOC = sb. Build (New fileinputstream (xmlfname ));
Ii. Dom Processing
Elements are operated on the same content class. Element class as Dom.
~ _~ (For other dom operations, see the jdom_api reference manual ).
3. Dom result processing (output/save)
Format fo = format. getcompactformat (); // obtain the output format
// Fo. setencoding ("gb2312 ");
Xmloutputter outputter = new xmloutputter (FO );
Filewriter write = new filewriter (xmlfname); // it can be in the form of printwriter (any writer stream ).
Outputter. Output (Doc, write/* Writer stream, see constructor */);
Write. Close ();
References:
JDOM V1.0 API online reference (http://www.jdom.org/docs/apidocs/index.html)
JDOM V1.0 download (http://www.jdom.org/dist/binary/jdom-1.0.zip)
JDOM Quick Start with XML Processing