/**
* XPath获取属性值
* @param root
* @param xPath
* @return
*/
public static String getXPathAttributeValue(Element root, String xPath){
XPathExpression<Attribute> pp = XPathFactory.instance().compile(xPath,Filters.attribute());
return pp.evaluateFirst(root).getValue();
} /**
* XPath获取节点值
* @param root 主节点
* @param xPath XPath字符串
* @return 没有找到返回null
*/
public static String getXPathText(Element root, String xPath){
XPathExpression<Text> pp = XPathFactory.instance().compile(xPath,Filters.text());
return pp.evaluateFirst(root).getText();
} /**
* XPath获取节点
* @param root 主节点
* @param xPath XPath字符串
* @return 没有找到返回null
*/
public static Element getXPathNode(Element root, String xPath){
XPathExpression<Element> pp = XPathFactory.instance().compile(xPath,Filters.element());
return pp.evaluateFirst(root);
} /**
* XPath获取节点集合
* @param root 主节点
* @param xPath XPath字符串
* @return XPath得到的节点集合
*/
public static List<Element> getXPathNodes(Element root, String xPath){
XPathExpression<Element> pp = XPathFactory.instance().compile(xPath,Filters.element());
return pp.evaluate(root);
}
JDom2的Xpath的获取关键在XPathExpression、XPathFactory身上.