【问题标题】:Creating an Xpath in java to select an attribute value based on another attribute value在java中创建一个Xpath来根据另一个属性值选择一个属性值
【发布时间】:2013-12-13 17:19:04
【问题描述】:

我知道这与很多问题有关,但我找不到像我这样使用 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);
     }
}

所以我希望能够选择计数,并且在将来,发生_at_time,使用此表达式“product[@name=" + Products[j] + "]attribute::count"。真的,我只需要知道在这种情况下这是否可能,或者我需要以不同的方式来解决这个问题。

【问题讨论】:

    标签: java xml dom xpath


    【解决方案1】:

    你想要xpath.compile("product[@name='" + Products[j] + "']/attribute::count");xpath.compile("product[@name='" + Products[j] + "']/@count");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      相关资源
      最近更新 更多