The Rud of XML

Source: Internet
Author: User
Tags add return string
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;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.