Java|XML 的 RUD
在工作中使用 XML 已经很长时间了,不过长久以来都是进行的读操作,或者在生成 XML 时完全使用 StringBuffer 来构造。进行完整的读取、添加、删除、修改还是最近的事情。在这里我采用的是 DOM4J,其实呢这些内容都很简单,如果愿意,各位大可直接参考官方的 Cookbook(http://www.dom4j.org/cookbook.html)和 Quick Start(http://www.dom4j.org/guide.html)。
对于给定的 XML 文件,其结构如下:
【Java|XML 的 RUD】 |
为以上 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; } } |
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; } |
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; } |
(请注意!引用、转贴本文应注明原作者:Rosen Jiang以及出处:http://blog.csdn.net/rosen)
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量