DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XML 的 RUD
XML 的 RUD
編輯:XML詳解     
在工作中使用 XML 已經很長時間了,不過長久以來都是進行的讀操作,或者在生成 XML 時完全使用 StringBuffer 來構造。進行完整的讀取、添加、刪除、修改還是最近的事情。在這裡我采用的是 DOM4J,其實呢這些內容都很簡單,如果願意,各位大可直接參考官方的 Cookbook(http://www.dom4j.org/cookbook.Html)和 Quick Start(http://www.dom4j.org/guide.Html)。
  
對於給定的 XML 文件,其結構如下: <?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>

  為以上 XML 文件構造 Propertys 類:

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;
}
}

讀取方法(返回包含 Propertys 的列表):
  
 public List getAll() {
 List list = new ArrayList();
 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()) {
  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;
}

添加新節點(成功返回 1 否則 0):
    
 public int saveProperty(Propertys property) {
 try {
InputStream 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;
} 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;
}

刪除節點:
  
 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;
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved