The simplest way to understand JDOM is Java + xml = JDOM. The following is an example of simple XML operations.
1. The following is an XML file used in the example:
/**
* In the C-drive root directory
* ABC. xml
**/
<? XML version = "1.0" encoding = "gb2312"?>
<Messages>
<Message id = "jhgjg">
<Title> jhgjg </title>
<Content> jhgjg </content>
<Email> jhsf-</Email>
</Message>
<Message id = "fdsa">
<Title> fdsa </title>
<Content> fasdf </content>
<Email> fsadfa </Email>
</Message>
</Messages>
2. xml operating program
/**
* Myjdom. Java
**/
Package com. test;
Import org. JDOM .*;
Import org. JDOM. Input .*;
Import org. JDOM. Output .*;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. util. List;
Public class myjdom {
Public static void main (string [] ARGs) throws exception {
Saxbuilder sb = new saxbuilder (); // create a constructor
Document Doc = sb. Build (New fileinputstream ("C: // ABC. xml"); // read the specified file
Element root = Doc. getrootelement (); // obtain the root node
List list = root. getchildren (); // put all the subnodes under the root node into the list
For (INT I = 0; I <list. Size (); I ++ ){
System. Out. println ("---------------------------");
Element item = (element) list. Get (I); // get the node instance
String name = item. getattribute ("name"). getvalue (); // get the attribute value
System. Out. println ("name -->" + name );
Element sub = item. getchild ("title"); // get the byte of the current node
String text = sub. gettext (); // gets the value of the current node.
System. Out. println ("title -->" + text );
Element sub2 = item. getchild ("content ");
String text2 = sub2.gettext ();
System. Out. println ("content -->" + text2 );
Element sub3 = item. getchild ("email ");
String text3 = sub3.gettext ();
System. Out. println ("email -->" + text3 );
}
}
}