Java xpath在单个调用中解析多个值
如何在一次调用中为多个路径获取xPath值 比如说Java xpath在单个调用中解析多个值,java,xml,xpath,Java,Xml,Xpath,如何在一次调用中为多个路径获取xPath值 比如说 <Message version="010" release="006" xmlns="http://www.ncpdp.org/schema/SCRIPT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Header> </Header> <Body> <CommunicationNumbers>
<Message version="010" release="006" xmlns="http://www.ncpdp.org/schema/SCRIPT" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
</Header>
<Body>
<CommunicationNumbers>
<Communication>
<Number>5551234444</Number>
<Qualifier>TE</Qualifier>
</Communication>
<Communication>
<Number>5551235555</Number>
<Qualifier>FX</Qualifier>
</Communication>
</CommunicationNumbers>
<Identification>
<FileID>616</FileID>
<DEANumber>AB123456</DEANumber>
<SocialSecurity>123456789</SocialSecurity>
</Identification>
<Specialty>A</Specialty>
<ClinicName>Therapy Department</ClinicName>
<Name>
<LastName>Xavior</LastName>
<FirstName>Charles</FirstName>
<MiddleName>C</MiddleName>
<Suffix>MD</Suffix>
</Name>
<Address>
<AddressLine1>888 ABC Drive</AddressLine1>
<AddressLine2>Suite 200</AddressLine2>
<City>Miami</City>
<State>FL</State>
<ZipCode>12345</ZipCode>
</Address>
</Body>
我想要的输出是一个串联的字符串555123444616a您可以尝试使用
XPathExpression expr = xpath.compile("//Communication/Number | //Identification/FileID");
它应该组合每个查询的结果。在我的(简单的)测试中,我得到了3个匹配项(2个用于Communication/Number
,1个用于Identification/FileID
)
已更新
预期结果是返回一个节点列表
,例如
NodeList nl = (NodeList)expr.evaluate(inputStr, XPathConstants.NODELIST);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String value = node.getTextContent();
System.out.println(value);
}
NodeList nl=(NodeList)expr.evaluate(inputStr,xpathcontents.NodeList);
对于(int index=0;index
自Java 7以来,节点列表被节点集取代:
NodeList nl = (NodeList)expr.evaluate(inputStr, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String value = node.getTextContent();
System.out.println(value);
}
NodeList nl=(NodeList)expr.evaluate(inputStr,XPathConstants.NODESET);
对于(int index=0;index
使用它只返回最后一个xpath表达式的值,即//Identification/FileID值616。它不连接并返回555123444616。对不起,我使用它返回节点列表
在我的测试中,它返回了第一个匹配项。我不认为STRING
能够做你想做的事情…所以nodelist将拥有所有的值??我可以迭代列表并获得单个值?XPathConstants.NODELIST
看起来不再正常,应该是NODESET
。看-
NodeList nl = (NodeList)expr.evaluate(inputStr, XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String value = node.getTextContent();
System.out.println(value);
}