Published on blog.csnd.net/zxub
There was nothing to do in the past two days, and I started to play with Java again. I wanted to write something to send an email and thought about what parameters to save. How can I save it? Suddenly think of an XML file. Well, use this.
After studying it, I feel that it is good to use dom4j. OK is it. Below I will post the code (modified version) and write it casually. There are still some shortcomings for your reference only:
/*
* Created on 15:26:04
* Modify no 2005-4-19 16:06:12
*/
/**
* @ Author zxub
*
*/
Import java. Io. file;
Import java. Io. filewriter;
Import java.net. malformedurlexception;
Import java. util. iterator;
Import java. util. List;
Import org. dom4j. Document;
Import org. dom4j. extends entexception;
Import org. dom4j. documenthelper;
Import org. dom4j. element;
Import org. dom4j. Io. saxreader;
Import org. dom4j. Io. xmlwriter;
Public class operaxml {
Private document = NULL;
Public static Boolean fileexist (string filename ){
Java. Io. File objfile = new java. Io. File (filename );
If (objfile. exists ()){
Return true;
} Else {
Return false;
}
}
Public void createxmlfile (string xmlfilename, string rootname ){
If (! Fileexist (xmlfilename )){
This.doc ument = incluenthelper. createdocument ();
Element element = this.doc ument. addelement (rootname );
// Add annotation element. addcomment (string)
// Add node element. addelement (string );
// Add the attribute content element. addattribute (name, value );
// Set the content element. settext (string );
// System. Out. println ("file created! ");
Savexmlfile (xmlfilename );
} Else {
System. Out. println ("file exist! ");
}
}
Public void addchild (string fatherpath, string childname, string childvalue ){
If (this.doc ument = NULL)
{
System. Out. println ("has not get XML file, add Err! ");
Return;
}
List list = this.doc ument. selectnodes (fatherpath );
Iterator iter = List. iterator ();
If (ITER. hasnext ()){
Element element = (element) ITER. Next ();
Element childelement = element. addelement (childname );
Childelement. settext (childvalue );
} Else {
System. Out. println ("Father node does not exist! Add error! ");
}
}
Public void modifynode (string xmlfilename, string nodepath,
String nodevalue, string newvalue ){
If (this.doc ument = NULL)
{
System. Out. println ("has not get XML file, modify Err! ");
Return;
}
List list = this.doc ument. selectnodes (nodepath );
Iterator iter = List. iterator ();
Boolean nodeexist = false;
While (ITER. hasnext ()){
Element element = (element) ITER. Next ();
If (element. gettext (). Equals (nodevalue )){
Element. settext (newvalue );
Nodeexist = true;
}
}
If (! Nodeexist ){
System. Out. println ("target node does not exist! Modify error! ");
}
}
Public void savexmlfile (string xmlfilename ){
If (this.doc ument = NULL)
{
System. Out. println ("has not get XML file, save Err! ");
Return;
}
Try {
/** Write the content in the document to the file */
Xmlwriter writer = new xmlwriter (New filewriter (new file (
Xmlfilename )));
Writer.write(this.doc ument );
Writer. Close ();
} Catch (exception ex ){
System. Out. println (ex. getmessage ());
}
}
Public void read (string xmlfilename ){
If (fileexist (xmlfilename )){
Saxreader reader = new saxreader ();
Try {
This.doc ument = reader. Read (new file (xmlfilename ));
} Catch (malformedurlexception e ){
System. Out. println (E. getmessage ());
} Catch (incluentexception e ){
System. Out. println (E. getmessage ());
}
} Else {
System. Out. println ("XML file does not exist, read error! ");
System. Exit (0 );
}
}
Public element getrootelement (){
If (this.doc ument = NULL)
{
System. Out. println ("has not get XML file, get root element Err! ");
Return NULL;
}
Return this.doc ument. getrootelement ();
}
Public String getnodevalue (string nodepath ){
If (this.doc ument = NULL)
{
System. Out. println ("has not get XML file, get node value Err! ");
Return NULL;
}
List list = this.doc ument. selectnodes (nodepath );
Iterator iter = List. iterator ();
Boolean nodeexist = false;
String nodevalue = NULL;
If (ITER. hasnext ()){
Element element = (element) ITER. Next ();
Nodevalue = element. gettext ();
Return nodevalue;
} Else {
System. Out. println ("target node does not exist! Read node error! ");
System. Exit (0 );
}
Return NULL;
}
Public void close ()
{
If (this.doc ument = NULL)
{
System. Out. println ("has not get XML file, close Err! ");
Return;
}
This.doc ument = NULL;
}
}