【问题标题】:xpath parsing multiple values in a single callxpath 在一次调用中解析多个值
【发布时间】:2014-07-03 02:57:01
【问题描述】:

如何在一次调用中获取多个路径的 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>
        <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>

我需要以下值:

:通讯/号码 :标识/文件ID :专业

在一次通话中。

对于我正在使用的单个值

 public static String getExpValue(final String xmlString, final String expression, final ServiceNamespaceContext nameSpace) throws XPathExpressionException {
    StringReader strReader = new StringReader(xmlString);
    InputSource inputStr = new InputSource(strReader);
    String result = null;
    try {
        final XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(nameSpace);

        final XPathExpression expr = xpath.compile(expression);
        result = (String) expr.evaluate(inputStr, XPathConstants.STRING);
    } finally {
        strReader = null;
        inputStr = null;
    }
    return result;
}

我想要的输出是单个连接的字符串 5551234444616A

【问题讨论】:

    标签: java xml xpath


    【解决方案1】:

    您可以尝试使用类似...

    XPathExpression expr = xpath.compile("//Communication/Number | //Identification/FileID");
    

    应该结合每个查询的结果。在我的(简单)测试中,我得到了 3 个匹配项(Communication/Number 有 2 个,Identification/FileID 有 1 个)

    更新

    预期的结果是返回一个NodeList,例如...

    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);
    }
    

    【讨论】:

    • 使用它只返回最后一个 xpath 表达式的值,即 //Identification/FileID 值 616 它不连接并返回 5551234444616
    • 对不起,我用它来返回NodeList
    • 在我的测试中,它返回了第一个匹配项。我不认为STRING 有能力做你想做的事......
    • 所以 nodelist 会有所有的值 ??我可以迭代列表并获取单个值?
    • XPathConstants.NODELIST 看起来不行了,应该是NODESET。见 - docs.oracle.com/javase/7/docs/api/javax/xml/xpath/…
    【解决方案2】:

    从 Java 7 开始,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);
    }    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 2023-03-27
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      相关资源
      最近更新 更多