package cn.itcast.dom4j; import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class DOM4JTest { public static void main(String[] args) throws DocumentException, IOException { xPathDemo3(); } /** * 使用XPath查找节点 找第二本书的书名 * @throws DocumentException */ public static void xPathDemo3() throws DocumentException { // 解析器 SAXReader saxReader = new SAXReader(); // 解析文档 Document document = saxReader.read("src/book.xml"); // 获取所有 书名 节点 @SuppressWarnings("unchecked") List<Element> elementsList = document.selectNodes("//书名"); System.out.println(elementsList.get(3).getText()); // 直接找到某本书的书名 Node node = document.selectSingleNode("//书名[@id='b1']"); System.out.println(node.getText()); } /** * 查询第一本书 * @throws DocumentException */ public static void demo1() throws DocumentException { // 创建解析器 SAXReader saxReader = new SAXReader(); // 解析文档 Document document = saxReader.read("src/book.xml"); // 先获得根节点 Element rooteElement = document.getRootElement(); // 查找根节点下的节点 String text = rooteElement.element("书").element("书名").getText(); // 打印输出 System.out.println(text); } /** * 添加元素 * @throws DocumentException * @throws IOException */ public static void add() throws DocumentException, IOException { // 创建解析器 SAXReader saxReader = new SAXReader(); // 解析文档 Document document = saxReader.read("src/book.xml"); // 获得根节点 Element rootElement = document.getRootElement(); // 找到要添加的节点 rootElement.element("书").addElement("书名").setText("武当剑法"); // 回写 完美格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); /*// 回写 压缩格式 OutputFormat outputFormat = OutputFormat.createCompactFormat();*/ outputFormat.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml"), outputFormat); writer.write(document); writer.close(); } /** * 修改元素:把 葵花宝典 改成 武当剑法 * @throws DocumentException * @throws IOException */ public static void modified() throws DocumentException, IOException { // 创建解析器 SAXReader saxReader = new SAXReader(); // 解析文档 Document document = saxReader.read("src/book.xml"); // 获得根节点 Element rooElement = document.getRootElement(); // 修改 rooElement.element("书").element("书名").setText("武当剑法"); // 回写 完美格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/book.xml"), outputFormat); xmlWriter.write(document); xmlWriter.close(); } /** * 删除第一本书的书名 * @throws DocumentException * @throws IOException */ public static void delete() throws DocumentException, IOException { // 解析器 SAXReader reader = new SAXReader(); // 解析文档 Document document = reader.read("src/book.xml"); // 获得根节点 Element root = document.getRootElement(); // 获得第一本书这个节点 Element element = root.element("书").element("书名"); // 删除书名 element.getParent().remove(element); // 回写 完美格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/book.xml"), outputFormat); xmlWriter.write(document); xmlWriter.close(); } }
XML解析之DOM4J
原创
©著作权归作者所有:来自51CTO博客作者foxspark的原创作品,如需转载,请与作者联系,否则将追究法律责任
上一篇:JAXP之SAX解析
下一篇:给网站添加小图标

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot集成Tess4J实现图片文字识别
SpringBoot集成Tess4J实现图片文字识别
SpringBoot Tess4j -
SpringBoot集成Resilience4J实现限流/重试/隔离学习
Springboot集成Resilience4j实现请求限流、重试和隔离等操作。
限流 重试 隔离 SpringBoot Resilience4j -
xml解析之DOM4J
DOM4J是一组XML操作的组件包,主要用来读写XML文件 生成xml文件:import java
xml dom4j java XML -
Dom4j 解析 XML(SAXParser)
Dom4j 解析 XML
dom4j xml dom4j 转 xml SAXParser