dom4j操作XML文件
来源:互联网 发布:python 获取微秒 编辑:程序博客网 时间:2024/05/22 00:02
package action.communication;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;/** * 使用dom4j操作XML文件的工具类 * * @author Administrater * */public class XMLUtil {/** * 在指定路径下创建一个xml文件 * * @param filePath * 路径,如C:\test.xml */public static void createXMLFile(String filePath) {File file = new File(filePath);// 判断该文件是否存在,不存在则创建if (!file.exists()) {try {file.createNewFile();//System.out.println("===");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Document document = DocumentHelper.createDocument();// 建立document对象,用来操作xml文件Element booksElement = document.addElement("books");// 建立根节点booksElement.addComment("This is a test for dom4j ");// 加入一行注释Element bookElement = booksElement.addElement("book");// 添加一个book节点bookElement.addAttribute("show", "yes");// 添加属性内容Element titleElement = bookElement.addElement("title");// 添加文本节点titleElement.setText("ajax in action");// 添加文本内容try {XMLWriter writer = new XMLWriter(new FileWriter(file));writer.write(document);writer.close();} catch (Exception e) {e.printStackTrace();}}// 修改节点的属性public static void modifyXMLFileNodeAttr() {String oldStr = "c:/test.xml";Document document = null;// 修改节点的属性try {SAXReader saxReader = new SAXReader(); // 用来读取xml文档document = saxReader.read(new File(oldStr)); // 读取xml文档List list = document.selectNodes("/books/book/@show");// 用xpath查找节点book的属性System.out.println(list);Iterator iter = list.iterator();while (iter.hasNext()) {Attribute attribute = (Attribute) iter.next();if (attribute.getValue().equals("yes")) {attribute.setValue("no");}}} catch (Exception e) {e.printStackTrace();}try {XMLWriter writer = new XMLWriter(new FileWriter(new File(oldStr)));writer.write(document);writer.close();} catch (Exception ex) {ex.printStackTrace();}}// 修改节点的内容public static void modifyXMLFileNodeText() {String oldStr = "c:/test.xml";Document document = null;// 修改节点的内容try {SAXReader saxReader = new SAXReader(); // 用来读取xml文档document = saxReader.read(new File(oldStr)); // 读取xml文档List list = document.selectNodes("/books/book/title");// 用xpath查找节点book的内容Iterator iter = list.iterator();while (iter.hasNext()) {Element element = (Element) iter.next();element.setText("哈哈哈");// 设置相应的内容}} catch (Exception e) {e.printStackTrace();}try {XMLWriter writer = new XMLWriter(new FileWriter(new File(oldStr)));writer.write(document);writer.close();} catch (Exception ex) {ex.printStackTrace();}}//删除某节点public static void removeNode() {String oldStr = "c:/test.xml";Document document = null;try {SAXReader saxReader = new SAXReader();// 用来读取xml文档document = saxReader.read(new File(oldStr));// 读取xml文档List list = document.selectNodes("/books/book");// 用xpath查找对象Iterator iter = list.iterator();while (iter.hasNext()) {Element bookElement = (Element) iter.next();// 创建迭代器,用来查找要删除的节点,迭代器相当于指针,指向book下所有的title节点Iterator iterator = bookElement.elementIterator("title");while (iterator.hasNext()) {Element titleElement = (Element) iterator.next();if (titleElement.getText().equals("哈哈哈")) {bookElement.remove(titleElement);}}}} catch (Exception e) {e.printStackTrace();}try {XMLWriter writer = new XMLWriter(new FileWriter(new File(oldStr)));writer.write(document);writer.close();} catch (Exception ex) {ex.printStackTrace();}}public static void main(String[] args) {//XMLUtil.createXMLFile("c:/test.xml");//XMLUtil.modifyXMLFileNodeAttr();//XMLUtil.modifyXMLFileNodeText();//XMLUtil.removeNode();}}
转载处不详
- Dom4j操作XML文件
- xml文件操作(dom4j)
- dom4j操作XML文件
- dom4j操作xml文件
- dom4j操作XML文件
- DOM4j操作xml文件
- Dom4j操作xml文件
- Dom4j操作XML文件
- dom4j操作 xml文件
- dom4j操作xml文件
- 使用dom4j操作xml文件!
- Java操作XML文件 dom4j
- xml文件操作(利用dom4j)
- dom4j操作xml文件详解
- 使用dom4j操作xml文件
- 使用dom4j操作xml文件
- dom4j操作xml文件整理
- 使用dom4j 操作xml文件
- 消息队列的使用
- BlowFish加密算法研究
- linux2.6中的platform和of_platform机制B
- CentOS:安装中文输入法
- 关于<C专家编程>的笔记
- dom4j操作XML文件
- 计算一个程序的运行时间
- nginx事件模块分析(一)
- linux platform A
- 收藏的Android非常好用的组件或者框架。
- 软件的实现过程
- nyoj 27水池问题
- 最新版 Android SDK 无法获取SDK包 的解决方案
- uva 208 Firetruck