XML 读取 写入 更新 Dom4J 操作
2012-01-11 14:49
344 查看
package cn.com.agree.fly.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * XML基本读取写入(dom4j操作) * @author 李海云 * @mail CloudComputing.cc@gmail.com * @date 2012-01-11 14:25 */ public class XMLReadWriter_Dom4J { public static void main(String[] args) throws DocumentException, IOException { writeXML("d:/product.xml"); readXML("d:/product.xml"); System.out.println("-----------"); modifyXML("d:/product.xml"); readXML("d:/product.xml"); } public static void writeXML(String filePath) throws IOException { Document document = DocumentHelper.createDocument();//创建xml文档对象 Element root = DocumentHelper.createElement("product");//创建根节点 document.setRootElement(root);//传入根目录 root.addAttribute("name","oracle");//添加根节点的属性 Element priceElement = root.addElement("price");//添加price标签元素 priceElement.setText("22.5");//设置元素值 Element descElement = root.addElement("desc"); descElement.setText("我是产品描述啊!"); descElement.addAttribute("title", "我是描述标题啊");//给元素设置属性 OutputFormat format = new OutputFormat(" ",true);//创建格式化模版对象 XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream(filePath),format);//创建xml写入器 xmlWriter2.write(document);//执行写入 xmlWriter2.flush(); xmlWriter2.close(); } public static void readXML(String filePath) throws DocumentException, IOException { File file = new File(filePath); SAXReader saxReader = new SAXReader();//创建读取器 Document document = saxReader.read(file);//读取xml到document对象中 Element rootElement = document.getRootElement();//获得文档根目录 System.out.println(rootElement.getName());//读取根目录名称 System.out.println(rootElement.attributeValue("name"));//读取更目录属性 Element priceElement = rootElement.element("price");//获得标签为price的节点 System.out.println(priceElement.getText());//获取其值 } private static void modifyXML(String filePath) throws IOException, DocumentException { File file = new File(filePath); SAXReader saxReader = new SAXReader();//创建读取器 Document document = saxReader.read(file);//读取xml到document对象中 Element rootElement = document.getRootElement();//获得文档根目录 rootElement.setName("user"); Attribute nameAttribute = rootElement.attribute("name"); nameAttribute.setText("haha"); Element priceElement = rootElement.element("price");//获得标签为price的节点 priceElement.setText("88.8");//修改值 Element descElement = rootElement.element("desc"); descElement.setText("我是描述2222222"); XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filePath));//创建写入器 xmlWriter.write(document);//写入最新document对象到文件中 xmlWriter.flush(); xmlWriter.close(); // //查看是否更新成功 // System.out.println(new SAXReader().read(new File(filePath)).getRootElement().element("price").getText()); } }
相关文章推荐
- XML入门之三:DOM4J的写入和读取操作
- Java操作读取写入文本TXT及XML文件内容
- Java配置文件Properties的读取、写入与更新操作
- Java配置文件Properties的读取、写入与更新操作
- php操作XML、读取数据和写入数据的实现代码
- Dom4j解析xml文件-操作节点和属性及写入其他xml文件
- Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- Dom4j解析xml文件-操作节点和属性及写入其他xml文件
- Java配置文件Properties的读取、写入与更新操作
- C#操作读取、写入XML文档的实用方法
- qt中关于xml的读取、写入、修改等操作
- Dom4j-操作节点和属性及写入其他xml文件
- 对Java配置文件Properties的读取、写入与更新操作
- Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- dom4j读取写入修改 xml
- (转)实现对Java配置文件Properties的读取、写入与更新操作
- 通过dom4j对xml文档的读取操作工具类封装
- 使用Dom4j读取和写入xml文件