Java xpath按节点检索对象列表

Java xpath按节点检索对象列表,java,xpath,Java,Xpath,如何使用Xpath按性别从以下xml中检索具有单个属性值的Psinfo对象: <?xml version="1.0" encoding="UTF-8"?> <Employees> <Employee> <Psinfo> <Name>Jhon</Name> <Gender xmlns:xs="http://www.w3.org/2001/XMLSch

如何使用Xpath按性别从以下xml中检索具有单个属性值的Psinfo对象:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
    <Employee>
        <Psinfo>
            <Name>Jhon</Name>
            <Gender xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"
                >Male</Gender>
            <Empsal xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"
                >95572553</Empsal>
        </Psinfo>
        <Psinfo>
            <Name>David</Name>
            <Gender xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"
                >Male</Gender>
            <Empsal xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"
                >1000000</Empsal>
        </Psinfo>
    </Employee>
</Employees>
以及以下Java代码:

     final NodeList list = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
                    for (int i = 0; i < list.getLength(); i++) {
                        final Node node1 = list.item(i);
                        // System.out.println(node.getNodeName().getTextContent());
final String result1 = (String) expr.evaluate(node1, XPathConstants.STRING); 
//but instead of getting individual tag values, giving whole  value as "JhonMale95572553
                    }

XML中没有名为
S36B
的元素,因此结果为空

更正XPath表达式后进行编辑:

/Employees/Employee/Psinfo[contains(Gender,'Male')]
//Psinfo[contains(Gender,'Male')]

表达式字符串是正确的。因此,您没有向我们展示的代码中一定存在错误(例如,如何构造文档、如何构建XPath表达式对象等)。

使用此XPath表达式:

/Employees/Employee/Psinfo[contains(Gender,'Male')]
//Psinfo[contains(Gender,'Male')]

试图通过以下方式获得结果:最终字符串result1=(String)expr.evaluate(node1,xpathcontents.String);但我并没有获取单个标记值,而是获取值为“JhonMale95572553”,试图通过以下方式获取结果:final String result1=(String)expr.evaluate(node1,XPathConstants.String);但是我没有得到单独的标记值,而是得到了“JhonMale95572553”@wero Code Review不用于调试的值。问题仅在其按预期工作时才在主题上。问题已更新,无法检索单个值。