| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
11年前发布

使用XPath解析xml文档

import org.jdom.Document;  import org.jdom.Element;  import org.jdom.input.SAXBuilder;  import org.jdom.xpath.XPath;    public class Xpather {      public static void testXPath(String filePath) throws Exception {          SAXBuilder sb = new SAXBuilder();          Document doc = sb.build(new FileInputStream(filePath));          Element root = doc.getRootElement(); //读取根节点          //返回resume节点下的所有子节点          XPath xPath = XPath.newInstance("/resume/*");          //返回文档中所有preOccupation节点          //XPath xPath = XPath.newInstance("//preOccupation");          //返回/resume/wife/preOccupation这个节点          //XPath xPath = XPath.newInstance("/resume/wife/preOccupation");          //返回名字preOccupation有属性peroid的节点          //XPath xPath = XPath.newInstance("/resume/wife/preOccupation[@period]");          //返回文档中所有preOccupation节点,并且有属性period          //XPath xPath = XPath.newInstance("//preOccupation[@period]");          //返回名字为preOccupation属性period='5-18'的节点。          //XPath xPath = XPath.newInstance("/resume/wife/preOccupation[@period='5-18']");          //返回文档中所有preOccupation节点,并且文本内容为皇子          //XPath xPath = XPath.newInstance("//preOccupation[text()='皇子']");          List list = xPath.selectNodes(root);          for (int i = 0; i < list.size(); i++) {              Element e = (Element) list.get(i);              System.out.println(e.getText());          }  //        Element e2 = (Element) xPath.selectSingleNode(root);  //        System.out.println(e2.getText());      }  }

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1386045558392.html
XPath XML操作类库