An example of JDOM JSP Javabean
JDOM makes Java's operation of the XML file very simple, and we can easily implement various operations of the XML file with very simple code.
We do two JavaBeans to encapsulate some basic operations: read an XML file (readxml.java) and write back an XML file (Writexml.java). The source code of these two files is as follows: readxml.java
Package XML;
Import Org.jdom. *;
Import Org.jdom.Output. *;
Import org.jdom.Input. *;
Import java.io. *;
Public class readxml {
PRIVATE DOCUMENT DOC = NULL;
Public retadxml () {}
Public String ReadXML (String XmlFileName) {
String strexc = ""
Try {
SAXBUILDER SB = New Saxbuilder ();
DOC = Sb.Build (New FileInputStream (XMLFileName));
} catch (exception e) {
strexc = e.tostring ();
}
Return (strexc);
}
Public document getXmldoc () {
Return (DOC);
}
}
This BEAB returns a Document type variable.
Writexml.java
Package XML;
Import Org.jdom. *;
Import Org.jdom.Output. *;
Import org.jdom.Input. *;
Import java.io. *;
Public class writexml {
Public Writexml () {}
Public String WriteXml (Document Doc, String XMLFileName) {
String strexc = ""
Try {
String indent = ""
Boolean newlines = true;
XMloutPutter Outp = New XMloutputter (Indenter, Newlines, "GB2312");
Outp.setTextTrim (TRUE);
Outp.output (DOC, New FileoutputStream (XMLFileName);
} catch (exception e) {
strexc = e.tostring ();
}
Return (strexc);
}
}
This bean writes a Document variable back to a specified XML file.