(四)使用dom4j方式操作dom4j是解析使用xpath时需要导入jaxen的jar包),dom4j最大的特色使用大量的接口。使用dom4j时需要导入dom4j-xxx.jar包。Attribute ——Attribute定义了Branc ...
(四)使用dom4j方式操作
dom4j是解析使用xpath时需要导入jaxen的jar包),dom4j最大的特色使用大量的接口。使用dom4j时需要导入dom4j-xxx.jar包。
Attribute ——Attribute定义了
Branch ——Branch为能够包含子节点的节点如
CDATA ——CDATA 定义了
CharacterData——CharacterData是一个标识接口,标识基于字符的节点。如CDATA,Comment, Text.
Comment—— Comment 定义了
Document—— 定义了
DocumentType—— DocumentType 定义
Element—— Element定义
ElementHandler ——ElementHandler定义了 Element 对象的处理器
ElementPath 被 ElementHandler 使用,用于取得当前正在处理的路径层次信息
Entity ——Entity定义
Node ——Node为所有的dom4j中
NodeFilter ——NodeFilter 定义了在dom4j节点中产生的一个滤镜或谓词的行为(predicate)
ProcessingInstruction ——ProcessingInstruction 定义
Text ——Text 定义
Visitor—— Visitor 用于实现Visitor模式.
XPath ——XPath 在分析一个字符串后会提供一个Xpath表达式
测试类代码:
1 public class Dom4jTest { 2 private Document doc; 3 4 @Before 5 public void setUp() throws Exception { 6 // 获得 7 InputStream is = Dom4jTest.class.getClassLoader().getResourceAsStream("books.); 8 // 创建SAXReader对象 9 SAXReader reader = new SAXReader(); 10 // 通过流对象解析 11 doc = reader.read(is); 12 } 13 14 /** 15 * 使用节点迭代器查询元素 16 * @throws Exception 17 */ 18 @Test 19 public void testRetrieve() throws Exception { 20 //获得文档的根节点 21 Element rootElement = doc.getRootElement(); 22 Iterator iterator = rootElement.elementIterator(); 23 while(iterator.hasNext()){ 24 //获得子节点,先转换为Element,有更多的方法 25 Element element = (Element) iterator.next(); 26 String value = element.attributeValue("category"); 27 //获得属性为CHILDREN的book节点 28 if("CHILDREN".equals(value)){ 29 //获得book节点的迭代器 30 Iterator childIterator = element.elementIterator(); 31 while(childIterator.hasNext()){ 32 Node node = (Node) childIterator.next(); 33 //获取子节点的名称 34 String name = node.getName(); 35 if("price".equals(name)){ 36 //获取节点的文本 37 String text = node.getText(); 38 System.out.println(text); 39 } 40 } 41 } 42 } 43 } 44 45 /** 46 * 使用访问器来读取 47 * @throws Exception 48 */ 49 @Test 50 public void testRetrieveByVisitor() throws Exception{ 51 //获得 52 InputStream is = Dom4jTest.class.getClassLoader().getResourceAsStream("books.); 53 // 创建SAXReader对象 54 SAXReader reader = new SAXReader(); 55 // 通过流对象解析 56 Document doc = reader.read(is); 57 // 获得文档的根节点 58 Element rootElement = doc.getRootElement(); 59 //创建自定义的Visitor对象 60 Visitor visitor = new DomVisitor(); 61 //使用visitor查询文档 62 rootElement.accept(visitor); 63 } 64 65 /** 66 * 创建新的 67 * @throws Exception 68 */ 69 @Test 70 public void testCreate() throws Exception{ 71 //第一种,创建Document的方法(org.dom4j.Document) 72 //第一种创建方式是对第二种创建方式的封装 73 Document document = DocumentHelper.createDocument(); 74 //第二种,创建Document的方法(org.dom4j.Document) 75 //Document doc = DocumentFactory.getInstance().createDocument(); 76 77 //创建根元素节点 78 Element rootElement = document.addElement("bookstore"); 79 //创建book节点及其子节点 80 Element bookElement = rootElement.addElement("book"); 81 bookElement.addAttribute("CATEGORY", "CODING"); 82 Element title=bookElement.addElement("title"); 83 title.setText("JAVA CODING"); 84 Element author=bookElement.addElement("author"); 85 author.setText("zs"); 86 Element year=bookElement.addElement("year"); 87 year.setText("2010"); 88 Element price=bookElement.addElement("price"); 89 price.setText("69"); 90 //打印到控制台 91 writeToConsole(document); 92 //输出到文件 93 writeToFile(document); 94 } 95 96 //输出到文件 97 private void writeToFile(Document document) throws IOException { 98 //创建美化格式 99 OutputFormat format=OutputFormat.createPrettyPrint();100 new 101 (new FileWriter(new File("abook.)),format);102 writer.write(document);103 //关闭writer就会清空缓存104 writer.close();105 }106 107 //打印到控制台108 private void writeToConsole(Document document) throws IOException {109 //创建输出字符流110 PrintWriter writer=new PrintWriter(System.out);111 document.write(writer);112 //关闭writer就会清空缓存,才会输出来113 writer.close();114 }115 116 /**117 * 使用XPath查找节点,xpath序号从1开始118 * 要使用dom4j的xpath支持,需要导入jaxen包119 */120 @Test121 public void testXPath() throws Exception{122 //获得属性web的book节点123 Node node = doc.selectSingleNode("//book[@category='WEB']");124 //获得所有的book节点125 List nodes = doc.selectNodes("//book");126 //获得第2个book节点127 Node secondNode = doc.selectSingleNode("//book[2]");128 //将129 String secondNode.as130 System.out.println(131 }132 133 @Test134 public void testStringTothrows Exception{135 String 136 "<author>J K. Rowling</author><year>2005</year><price>29.99</price></book>";137 //转换字符串为138 Document document = DocumentHelper.parseText(139 //打印到控制台140 writeToConsole(document);141 }142 143 /**144 * 删除指定的节点145 * @throws Exception146 */147 @Test148 public void testDelete() throws Exception{149 //获得最后一个book节点150 Node node = doc.selectSingleNode("//book[4]");151 //获得最后一个book节点的父节点152 Element parent = node.getParent();153 //删除最后一个book节点,删除需要使用父节点来删除,删除成功则返回true154 Boolean flag=parent.remove(node);155 System.out.println(flag);156 //打印到控制台157 writeToConsole(doc);158 }159 160 /**161 * 修改指定的节点162 * @throws Exception163 */164 @Test165 public void testUpdate() throws Exception{166 //获得第一个book节点的price和year167 Node yearNode = doc.selectSingleNode("//book[1]/year");168 yearNode.setText("2015");169 Node priceNode = doc.selectSingleNode("//book[1]/price");170 priceNode.setText("59.00");171 //修改后的172 writeToConsole(doc);173 }174 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:XML编程总结(四)——使用dom4j方式操作xml
关键词:xml
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。