标签:enc ping pac select 演示 ace tno style new
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"> 3 <servlet> 4 <servlet-name>HelloMyServlet</servlet-name> 5 <servlet-class>cn.itcase.HelloMyServlet</servlet-class> 6 </servlet> 7 <servlet-mapping> 8 <servlet-name>HelloMyServlet</servlet-name> 9 <url-pattern>/hello</url-pattern> 10 </servlet-mapping> 11 </web-app>
1 //创建对象 2 SAXReader saxReader = new SAXReader(); 3 //将xml文档加载到内存当中 4 Document document = saxReader.read("D:\\workspace\\mylanqiao\\src\\day8_26\\demo.xml"); 5 //获取根节点 6 Element root = document.getRootElement(); 7 //通过根节点操作其他节点 8 List<Element> elements = root.elements(); 9 //操作1:获取所有子元素 10 for (Element ele : elements) { 11 //操作2:获取元素的指定属性内容 12 //操作3:获取标签体,遍历list获取每一个子元素 13 /*String elementText = ele.elementText("servlet-name"); 14 System.out.println(elementText); 15 */ 16 //获取另一个标签体 17 System.out.println(ele.elementText("url-pattern")); 18 /* 19 * 第一个标签没有就返回一个空值 20 * null 21 /hello*/ 22 } 23 //获取根节点的属性 24 System.out.println(root.attributeValue("version"));
1 //加载dom树 2 Document document = new SAXReader().read("D:\\workspace\\mylanqiao\\src\\day8_26\\demo.xml"); 3 //获取多个节点 4 // List<Element> selectNodes = document.selectNodes("/web-app/servlet/servlet-name"); 5 // Element ele = selectNodes.get(0); 6 // System.out.println(ele.getText()); 7 // 8 //获取某个节点 9 Element element = (Element) document.selectSingleNode("//servlet/servlet-name"); 10 //返回值是Node,强转成element,//代表不从根节点开始找 11 System.out.println(element.getText());
标签:enc ping pac select 演示 ace tno style new
原文地址:http://www.cnblogs.com/anzhi/p/7447454.html