2013-12-13 188 views
0

我知道這與許多問題有關,但我找不到使用xml佈局的示例,例如我的。在java中創建Xpath以根據另一個屬性值選擇屬性值

<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,這種表達「產品[@名稱=」 +產品[J] +「]屬性::計數」。真的,我只是需要知道在情況下這是否可能,或者我需要以不同的方式來實現。

回答

1

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

+0

完美,謝謝! – Chad

相關問題