Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中创建Xpath以基于另一个属性值选择属性值_Java_Xml_Dom_Xpath - Fatal编程技术网

在java中创建Xpath以基于另一个属性值选择属性值

在java中创建Xpath以基于另一个属性值选择属性值,java,xml,dom,xpath,Java,Xml,Dom,Xpath,我知道这与许多问题有关,但我找不到一个使用像我这样的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

我知道这与许多问题有关,但我找不到一个使用像我这样的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);
     }
}
for(int j=0;j

因此,我希望能够选择计数,并且在将来,使用这个表达式“product[@name=“+Products[j]+”]attribute::count”在_时间发生。实际上,我只需要知道在这种情况下这是否可行,或者我需要以另一种方式来解决这个问题。

您需要
xpath.compile(“product[@name=”“+Products[j]+”]/attribute::count”)
or
xpath.compile(“product[@name=”+Products[j]+“]/@count”)