From: http://www.xjtuhx.com/bbs/dispbbs.asp? Boardid = 6 & id = 32 & page = 1
JDOM-based XML operations
Create XML
1) dependent classes:
Org. JDOM. Document;
Org. JDOM. element;
Org. JDOM. Output. xmloutputter;
Org. JDOM. Input. jdomexception;
Java. Io. ioexception;
2) usage:
Public void write (){
// Create a Document Object
Document document = new document ();
// Create an Element Object
Element root = new element ("beans ");
// Create the root node root
Document. setrootelement (Root );
// Create the node and value under root
Element bean = new element ("Bean ");
Element id = new element ("ID ");
Id. settext ("1000 ");
Bean. addcontent (ID );
Element name = new element ("name ");
Name. settext ("baby ");
Bean. addcontent (name );
Element age = new element ("Age ");
Age. settext ("26 ");
Bean. addcontent (AGE );
// Obtain the default JDOM format
Format = format. getprettyformat ();
// Set the encoding format to "gb2312"
Format. setencoding ("gb2312 ");
Root. addcontent (bean );
// Create an output object
Xmloutputter out = new xmloutputter ();
// Use the output format "format"
Out. setformat (format );
Try {
Out. Output (document, system. Out );
// Output the Document Object to D:/userdate. xml
Out. Output (document, new fileoutputstream ("D:/userdate. xml "));
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
Parse XML
1) dependent classes:
Org. JDOM. Input. saxbuilder;
Org. JDOM. Input. jdomexception;
Java. Io. bytearrayinputstream;
Java. Io. fileinputstream;
Java. Io. ioexception;
2) usage:
/**
* Generate a document object from an XML string.
*/
Public void stringtest (){
String xml = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Beans> <bean> <ID> 1000 </ID> <Name> baby </Name> <age> 26 </age> </bean> </beans> ";
Try {
Bytearrayinputstream input = new
Bytearrayinputstream (XML. getbytes ("UTF-8 "));
Try {
Document document = new saxbuilder (). Build (input );
Xmloutputter out = new xmloutputter ();
Format = format. getprettyformat ();
Format. setencoding ("gb2312 ");
Out. setformat (format );
Out. Output (document, system. Out );
} Catch (jdomexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
}
}
/**
* Generate a document object from a file.
*/
Public void read (){
Saxbuilder sb = new saxbuilder ();
Try {
Document document = sb. Build (New
Fileinputstream ("D:/userdate. xml "));
Element root = Document. getrootelement ();
Xmloutputter out = new xmloutputter ();
Format = format. getprettyformat ();
Format. setencoding ("gb2312 ");
Out. setformat (format );
Out. Output (root. getchild ("Bean"). getchild ("name"), system. Out );
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (jdomexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
/**
* Generate a document object from the resource file.
* @ Param sresourcepath string
*/Comoohla/account/permission. xml
* @ Return document
*/
Public static document getresourcedocument (string sresourcepath ){
Document Doc = NULL;
Try {
Inputstream in =
New xmlutils (). getclass (). getresourceasstream (sresourcepath );
Doc = new saxbuilder (). Build (in );
}
Catch (jdomexception ex ){
}
Return Doc;
}
/**
*
*/
Public static void parse (){
String sfilepath = "/comoohla/account/permission. xml ";
Document Doc = getresourcedocument (sfilepath );
//! Modules-Permission
Element root = Doc. getrootelement ();
//! Module
Java. util. List children = root. getchildren ();
/**
* ID, name, mask
*/
For (INT I = 0; I <children. Size (); I ++ ){
Element module = (element) children. Get (I );
Int id = oohla. util. charkit. getint (module. getchildtext ("ID "));
String name
= Oohla. util. charkit. getstring (module. getchildtext ("name "));
Int mask = oohla. util. charkit. getint (module. getchildtext ("Mask "));
}
}
Methods for viewing the element tree
Element root = Doc. getrootelement (); // obtain the root element
List allchildren = root. getchildren (); // obtain a list of all child elements
List namedchildren = root. getchildren ("name"); // obtain the list of child elements with the specified name
Element child = root. getchild ("name"); // obtain the first child element of the specified name
How to manage child elements
List allchildren = root. getchildren ();
Allchildren. Remove (3); // deletes the fourth child element.
Allchildren. removeall (root. getchildren ("Jack"); // delete a child named "Jack" // Element
Root. removechildren ("Jack"); // convenient Writing Method
Allchildren. Add (new element ("Jane"); // Add
Root. addcontent (new element ("Jane"); // convenient Writing Method
Allchildren. Add (0, new element ("first "));