Web effects p/jsp.html target=_blank >jsp Tutorial Add, delete, read the content code of the specified tag in the XML file
Package XML;
Import Java.io.file;
Import java.util.list;
Import org.dom4j.document;
Import org.dom4j.documentexception;
Import org.dom4j.element;
Import Org.dom4j.qname;
Import Org.dom4j.io.saxreader;
public class Dom4jtest {
Private Saxreader reader;
Private document document;
private static string path = "C:/dom4jsample1.xml";
Public dom4jtest (String str) throws Documentexception {
reader = new Saxreader ();
Document = Reader.read (new file (str));
}
public static void Main (string[] args) throws Documentexception {
Todo auto-generated method stub
Dom4jtest test = new Dom4jtest (path);
Test.getinfo ();
}
public void GetInfo () {
Element root = Document.getrootelement ();
QName 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 to File
private void WriteFile (document DOC) throws IOException {
FileWriter out = new FileWriter (path);
Doc.write (out);
Out.flush ();
Out.close ();
}