Dom4j解析xml总结
2016-06-06 22:59
369 查看
首先需要导入jar包,下面给出jar包
xml
package com.wnl.parselxml; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Date; import java.util.Iterator; import java.util.List; 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; import org.junit.Test; public class ChinaXml { /** * 快速入门 * @throws Exception */ @Test public void test1() throws Exception { // 获取SAX解析器 SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File("src/china.xml")); // 获取根节点 Element root = document.getRootElement(); // String cityElement= element.element("city").getText(); // System.out.println(cityElement); /** * 遍历所有节点,即root的子节点 */ System.out.println("====遍历所有节点,即root的子节点==="); for (Iterator i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); System.out.println(element.getName()); } /** * 遍历root节点的属性 */ System.out.println("======遍历root下指定名称的节点======"); for (Iterator i = root.elementIterator("attraction"); i.hasNext();) { Element element = (Element) i.next(); System.out.println(element.getName()); } /** * 输出root子节点的所有属性 */ System.out.println("============="); for (Iterator i = root.attributeIterator(); i.hasNext();) { Attribute attribute = (Attribute) i.next(); System.out.println("name=" + attribute.getName() + " value=" + attribute.getData()); } } @Test // 取出同名节点的内容 public void test2() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/china.xml")); Element root = document.getRootElement(); List<Element> list = root.elements("attraction"); for (int i = 0; i < list.size(); i++) { Element attracEle = list.get(i); String place = attracEle.element("place").getTextTrim(); System.out.println(place); } } /** * 在<attraction>下增加新的节点 * DocumentHelper 可以增加节点、属性 * @throws Exception */ @Test public void test3() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/china.xml")); //获取文档根节点 Element root = document.getRootElement(); //取得attraction节点 Element attracEle = root.element("attraction"); //在attraction节点添加新的节点 Element dateEle = DocumentHelper.createElement("date"); dateEle.setText("八月"); dateEle.setAttributeValue("month", "12"); attracEle.add(dateEle); //更新xml // Writer writer = new FileWriter(new File("src/china.xml"));//出现乱码 //方法 1 // Writer writer=new OutputStreamWriter(new FileOutputStream("src/china.xml"), "gb2312"); // document.write(writer); // writer.close(); //方法 2 /* * XMLWriter默认使用utf-8编码 */ // XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("src/china.xml")); OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("src/china.xml"),format); xmlWriter.write(document); } /** * 修改节点 * @throws Exception */ @Test public void test4() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/china.xml")); //获取文档根节点 Element root = document.getRootElement(); Element dateEle=root.element("attraction").element("date"); dateEle.setText(new Date()+""); //更新xml OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("src/china.xml"),format); xmlWriter.write(document); } /** *删除节点 * @throws Exception */ @Test public void test5() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/china.xml")); //获取文档根节点 Element root = document.getRootElement(); Element cityEle=root.element("city"); cityEle.getParent().remove(cityEle); //更新xml OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("src/china.xml"),format); xmlWriter.write(document); } /** * 在指定位置添加节点 * @throws Exception */ @Test public void test6() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/china.xml")); //获取文档根节点 Element root = document.getRootElement(); List<Element> listEle= root.elements(); for(int i=0;i<listEle.size();i++){ Element noteEle=DocumentHelper.createElement("记录"); noteEle.setText("这是第"+i+"个景点"); listEle.get(i).add(noteEle); } //更新xml OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("src/china.xml"),format); xmlWriter.write(document); } /** * 操作属性 * @throws Exception */ @Test public void test7() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/china.xml")); //获取文档根节点 Element root = document.getRootElement(); Element attracEle=root.element("attraction"); /** * 添加属性 */ // Attribute attribute= DocumentHelper.createAttribute(attracEle, "国家", "中国"); // attracEle.add(attribute); /** * 修改属性 */ // Attribute attribute=attracEle.attribute("国家"); // attribute.setValue("北京"); // attribute.setName("capital"); 无法修改名字 /** * 查找属性 */ // Attribute attribute=attracEle.attribute("国家"); // String name=attribute.getName(); // String value=attribute.getValue(); // System.out.println("name="+name+" "+"value="+value); /** * 删除属性 */ Attribute attribute=attracEle.attribute("国家"); attracEle.remove(attribute); //更新xml OutputFormat format=OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlWriter=new XMLWriter(new FileOutputStream("src/china.xml"),format); xmlWriter.write(document); } /** * xpath * @throws Exception */ @Test public void test8() throws Exception{ // 获取SAX解析器 SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File("src/china.xml")); // 获取根节点 Element root = document.getRootElement(); List<Element> eleList=root.selectNodes("//weather"); System.out.println(eleList); } }
xml
<?xml version="1.0" encoding="utf-8"?> <country name="中国" id="haahh"> <attraction id="1"> <place>云贵高原</place> <weather>晴朗</weather> <traffic>高铁</traffic> <date month="12">Sun Jun 05 22:41:10 CST 2016</date> <记录>这是第0个景点</记录> </attraction> <attraction> <place>上海浦东</place> <weather>多云</weather> <traffic>大巴</traffic> <记录>这是第1个景点</记录> </attraction> <attraction> <place>黑龙江</place> <weather>晴朗</weather> <traffic>京广高铁</traffic> <记录>这是第2个景点</记录> </attraction> </country>
相关文章推荐
- HNOI2014 世界树 基于虚树的树形动态规划
- java匿名内部类
- 【LeetCode】011 Swap Nodes in Pairs 两两换位
- Android的线程和线程池
- Linq使用Group By 1
- hibernate框架知识
- fseek的坑(跨平台注意)
- Servlet 生命周期、工作原理
- svn "cannot set LC_CTYPE locale" 问题以及LANG, LC_CTYPE, LC_ALL值的设置
- java多线程常见问题和解析
- 高可用系统开发可能遇到的问题
- 【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
- Spark之DataFrame通过编码创建
- c++13周实验:输入/输出流的综合应用
- machine learning in action 之一 —— 环境配置
- DOM-----document对象
- Oracle一列的多行数据拼成一行显示字符
- LeetCode 141 Linked List Cycle(循环链表)(hash / set)
- java : tomcat 6.0 应用 JSTL 正确做法
- for 循环 and while 循环(三)