XPath 入门

[quote]xpath可以快速定位获取XML文件中指定属性和值,jdk包含拉所有需要的类[/quote]

1. 帮助类
public class XpathUtil {
private static final Log logger = LogFactory.getLog(XpathUtil.class.getName());
private Document doc;

public XpathUtil(File file) {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringElementContentWhitespace(true);
domFactory.setNamespaceAware(false);
try {
DocumentBuilder builder = domFactory.newDocumentBuilder();
this.doc = builder.parse(file);
} catch (Exception e) {
e.printStackTrace();
}
}

public List<String> getValuesByXpath(String expString) {
List<String> values = new ArrayList<String>();

try {
String expArray[]= expString.split(Constants.COMMA);
for(String exp :expArray){
XPath xpath = XPathFactory.newInstance().newXPath();

XPathExpression expr = xpath.compile(exp);
Object result = expr.evaluate(this.doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
String nodeValue = nodes.item(i).getTextContent();
values.add(nodeValue);
}

}

} catch (XPathExpressionException e) {
logger.error("", e);
} catch (DOMException e) {
logger.error("", e);
}
return values;
}

}


2. XML Sample
<?xml version="1.0" encoding="UTF-8"?>
<p:ProcessLifeCycle xmlns:p="http://www.example.org/draft_2/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/draft_2/ metaData.xsd ">

<TableList>
<Table>
<name>tab.eq_exec_detail</name>
<p:CommonRefAttrElem>
<name>table_name</name>
<value>eq_exec_detail</value>
</p:CommonRefAttrElem>
<p:CommonRefAttrElem>
<name>primary_key</name>
<value>exec_did</value>
</p:CommonRefAttrElem>
<p:CommonRefAttrElem>
<name>record</name>
<record>
<name>rec.eq_exec_detail</name>
<description>exec detail record in ocean format. Table tab.eq_exec_detail</description>
<p:CommonRefAttrElem>
<name>exec_did</name>
<type>bigint</type>
<length>8</length>
<value>not_possdup</value>
<nullFlag>N</nullFlag>
</p:CommonRefAttrElem>
<p:CommonRefAttrElem>
<name>sys_exec_ver</name>
<type>integer</type>
<length>4</length>
<value>possdup</value>
<nullFlag>Y</nullFlag>
</p:CommonRefAttrElem>
<p:CommonRefAttrElem>
<name>side</name>
<type>varchar</type>
<length>20</length>
<value>possdup</value>
<nullFlag>Y</nullFlag>
</p:CommonRefAttrElem>
</record>
</p:CommonRefAttrElem>

</Table>
</TableList>
</p:ProcessLifeCycle>


3. 测试类
public class XpathUtilTest {
@Test
public void testRetrieveXpath() throws Exception {
XpathUtil util = new XpathUtil(new File("test.xml"));
List<String> values = util.getValuesByXpath("//TableList/Table/CommonRefAttrElem/record/CommonRefAttrElem/name");
System.out.println(values);
}
}


输出
[quote][exec_did, sys_exec_ver, side]
[/quote]

4. xpath获取eclipse插件,解压到dropin文件夹。
[url]http://code.google.com/p/eclipse-xpath-evaluation-plugin/downloads/detail?name=eclipse-xpath-evaluation-plugin-1.2.3.zip[/url]

点击xml节点,可以获取XPATH
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值