XML has been working with XML for a long time, but it has long been a read operation, or it is constructed entirely using stringbuffer when generating XML. Complete read, add, delete, modify or recent events. Here I use is dom4j, in fact, these contents are very simple, if you like, you can directly refer to the official Cookbook (http://www.dom4j.org/cookbook.html) and Quick Start (http:// www.dom4j.org/guide.html).
For a given XML file, the structure is as follows: <?xml version= "1.0" encoding= "GBK"?>
<propertysets>
<propertset name= "Rea_faculty" description= "Team" >
<field>10290</field>
</propertset>
<propertset name= "Faculty_lea" description= "another Team" >
<field>10286</field>
</propertset>
<propertset name= "Office" description= "teams" >
<field>10287</field>
</propertset>
</propertysets>
Constructs the Propertys class for the above XML file:
public class Propertys {
private String name;
Private String description;
Private String field;
Public String getdescription () {
return description;
}
public void SetDescription (String description) {
this.description = description;
}
Public String GetField () {
return field;
}
public void SetField (String field) {
This.field = field;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
}
Read method (returns a list containing Propertys):
Public List getAll () {
list = new ArrayList ();
try { br> InputStream is = GetClass (). getResourceAsStream ("/navigation.xml");
Saxreader reader = new Saxreader ();
Document document = Reader.read (IS);
Element root = document.getrootelement ();
Iterator LV = root.elementiterator ("Propertset");
Element el = null;
while (Lv.hasnext ()) {
Propertys property=new Propertys ();
El = (Element) lv.next ();
Property.setname (El.attributevalue ("name"));
Property.setdescription (El.attributevalue ("description"));
Property.setfield (El.elementtext ("field"));
List.add (property);
}
catch (Exception e) {
E.printstacktrace ();
}
return list;
}
Add a new node (successfully returns 1 otherwise 0):
public int Saveproperty (Propertys) {
try {
Inputstre AM is = GetClass (). getResourceAsStream ("/navigation.xml");
Saxreader reader = new Saxreader ();
Document document = Reader.read (IS);
Element root = document.getrootelement ();
Root.addelement ("Propertset")
. AddAttribute ("name", Property.getname ())
. AddAttribute ("description", Property.getdescription ())
. AddElement ("field"). AddText (Property.getfield ());
OutputFormat Outformat = Outputformat.createprettyprint ();
Outformat.setencoding ("GBK");
FileWriter out = new FileWriter (
System.getproperty ("User.dir")
+ "/web/web-inf/classes/navigation.xml");
XMLWriter writer=new XMLWriter (Out,outformat);
Writer.write (document);
Writer.close ();
return 1;
} catch (Exception e) {
E.printstacktrace ();
}
return 0;
}
Update node (lookup by name attribute):
public int updateproperty (String Pro,propertys property) {
try {
InputStream is = GetClass (). getResourceAsStream ("/navigation.xml");
Saxreader reader = new Saxreader ();
Document document = Reader.read (IS);
Element root = document.getrootelement ();
Iterator LV = root.elementiterator ("Propertset");
Element el = null;
while (Lv.hasnext ()) {
el = (Element) lv.next ();
if (El.attributevalue ("name"). Equals (pro) {
El.setattributevalue ("name", Property.getname ());
El.setattributevalue ("description", Property.getdescription ());
El.element ("field"). SetText (Property.getfield ());
}
}
OutputFormat Outformat = Outputformat.createprettyprint ();
Outformat.setencoding ("GBK");
FileWriter out = new FileWriter (
System.getproperty ("User.dir")
+ "/web/web-inf/classes/navigation.xml");
XMLWriter writer=new XMLWriter (Out,outformat);
Writer.write (document);
Writer.close ();
return 1;
catch (Exception e) {
E.printstacktrace ();
}
return 0;
}
To delete a node:
public int DelProperty (String Pro) {
try {
InputStream is = GetClass (). getResourceAsStream ("/navigation.xml");
Saxreader reader = new Saxreader ();
Document document = Reader.read (IS);
Element root = Document.getrootelement ();
Iterator LV = root.elementiterator ("Propertset");
Element el = null;
while (Lv.hasnext ()) {
El = (Element) lv.next ();
if (El.attributevalue ("name"). Equals (pro)) {
El.detach ();
}
}
OutputFormat Outformat = Outputformat.createprettyprint ();
Outformat.setencoding ("GBK");
FileWriter out = new FileWriter (
System.getproperty ("User.dir")
+ "/web/web-inf/classes/navigation.xml");
XMLWriter writer=new XMLWriter (Out,outformat);
Writer.write (document);
Writer.close ();
return 1;
catch (Exception e) {
E.printstacktrace ();
}
return 0;
}