Add, delete, and read the content code of the specified tag in the xml file.
Webpage special effect p/jsp.html target = _ blank> added, deleted, and read the content code specified in the xml file
Package xml;
Import java. io. file;
Import java. util. list;
Import org.dom4j.doc ument;
Import org.dom4j.doc umentexception;
Import org. dom4j. element;
Import org. dom4j. qname;
Import org. dom4j. io. saxreader;
Public class dom4jtest {
Private saxreader reader;
Private document;
Private static string path = "c:/dom4jsample1. xml ";
Public dom4jtest (string str) throws into entexception {
Reader = new saxreader ();
Document = reader. read (new file (str ));
}
Public static void main (string [] args) throws extends entexception {
// Todo auto-generated method stub
Dom4jtest test = new dom4jtest (path );
Test. getinfo ();
}
Public void getinfo (){
Element root = document. getrootelement ();
Qname = new qname ("ip ");
List <element> list = root. elements (qname );
For (int I = 0; I <list. size (); I ++ ){
System. out. println (list. get (I). gettext ());
}
}
}
Public void add (string add) throws ioexception {
Element root = document. getrootelement ();
Element e = root. addelement ("ip ");
E. settext (add );
Writefile (document );
}
// Write a file
Private void writefile (document doc) throws ioexception {
Filewriter out = new filewriter (path );
Doc. write (out );
Out. flush ();
Out. close ();
}