我知道这与许多问题有关,但我找不到像我一样使用xml布局的例子。
<product name="ANALYTICS" count="24" occurred_at_time="1386648300000">
<user_type name="Unknown" count="24" occurred_at_time="1386648300000">
<session_source name="Web" count="24" occurred_at_time="1386648300000"/>
</user_type>
</product>
<product name="CARSWELL" count="492" occurred_at_time="1386651900000">
<user_type name="External" count="492" occurred_at_time="1386651900000">
<session_source name="Web" count="492" occurred_at_time="1386651900000"/>
</user_type>
<user_type name="Internal" count="19" occurred_at_time="1386622200000">
<session_source name="UNKNOWN" count="12" occurred_at_time="1386624300000" />
<session_source name="Web" count="10" occurred_at_time="1386604200000" />
</user_type>
</product>
我正在遍历一个属性文件,该文件指定了我们在xml中感兴趣的产品,并且很难创建正确的表达式。
for (int j = 0; j < Products.length; j++) {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
try {
XPathExpression expr = xpath.compile("product[@name=" + Products[j] + "]attribute::count");
CompareNextWeb = expr.evaluate(doc);
System.out.println(CompareNextWeb);
} catch (XPathExpressionException ex) {
Logger.getLogger(NextReport.class.getName()).log(Level.SEVERE, null, ex);
}
}
所以我希望能够选择计数,以及将来的occured_at_time,使用这个表达式“product [@ name =”+ Products [j] +“] attribute :: count”。真的,我只是需要知道在这种情况下这是否可行,或者我需要以不同的方式来实现。
答案 0 :(得分:1)
您需要xpath.compile("product[@name='" + Products[j] + "']/attribute::count");
或xpath.compile("product[@name='" + Products[j] + "']/@count");
。