我正在尝试使用XPath从请求中提取“ PartyID”。该请求采用XML形式。

这是XML:

<?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<s1:invokerules xmlns:s1="http://rules.kmtool.abc.com"><s1:arg0><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
   <kbdInitiateRequest>
    <kmTestHeader>
        <MessageId>USER1_MSG1</MessageId>
            <TestDate>08/07/2008 07:34:15</TestDate>
            <TestReference>
            <ConductorReference>
                <InvokeIdentifier>
                    <RefNum>USER1_Ref1</RefNum>
                </InvokeIdentifier>
            </ConductorReference>
        </TestReference>
        <TestParty>
            <ConductorParty>
                <Party PartyID="123456789" AgencyID="DUNS">
                    <TestContact>
                        <DetailedContact>
                                                <ContactName>Michael Jackson</ContactName>
                            <Telephone>02071059053</Telephone>
                            <TelephoneExtension>4777</TelephoneExtension>
                            <Email>Michal.Jackson@Neverland.com</Email>
                            <Title>Mr</Title>
                            <FirstName>Michael</FirstName>
                            <Initials>MJ</Initials>
                        </DetailedContact>
                    </TestContact>
                </Party>
            </ConductorParty>
            <PerformerParty>
                <Party PartyID="987654321" AgencyID="DUNS">
                </Party>
            </PerformerParty>
        </TestParty>
    </kmTestHeader>
    <kmToolMessage>
        <controlNode>
            <userRequest>INITIATE</userRequest>
        </controlNode>
        <customer>
            <circuitID>000111333777</circuitID>
    </customer>
</kmToolMessage>
</kbdInitiateRequest>

]]></s1:arg0>
</s1:invokerules>
</soapenv:Body>
</soapenv:Envelope>


我的Java代码中有一个名为getPartyId()的方法。此方法应从XML中提取PartyID。但是,无论我使用哪种XPath查询,都无法获得此方法来返回PartyID,这是我需要帮助的地方。

这是getPartyId方法:

private String getPartyId(String xml) throws XPathExpressionException
    {
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        xpath.setNamespaceContext(new NamespaceContext() {
            public String getNamespaceURI(String prefix) {
                if (prefix == null) throw new NullPointerException("Null prefix");
                else if ("SOAP-ENV".equals(prefix)) return "http://schemas.xmlsoap.org/soap/envelope/";
                else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
                return XMLConstants.NULL_NS_URI;
            }

            public String getPrefix(String uri) {
                throw new UnsupportedOperationException();
            }

            public Iterator getPrefixes(String uri) {
                throw new UnsupportedOperationException();
            }
        });

        XPathExpression expr = xpath.compile("/SOAP-ENV:Envelope/SOAP-ENV:Body/*/*/*/*/*/*/*/*/*/*/*[local-name()='PartyID']/text()");

        InputSource source = new InputSource(new StringReader(xml));

        String dunsId = (String) expr.evaluate(source,XPathConstants.STRING);

        return dunsId;
    }


我相信问题在于XPathExpression:

XPathExpression expr = xpath.compile("/SOAP-ENV:Envelope/SOAP-ENV:Body/*/*/*/*/*/*/*/*/*/*/*[local-name()='PartyID']/text()");


我已经尝试了许多“ expr”的替代方法,但是这些都没有用。有人知道吗?

最佳答案

由于您需要解析的xml位于CDATA块中,因此您需要在访问其中的数据之前重新解析s1:arg0的值。

您将需要2个步骤


您将需要访问arg0命名空间中的http://rules.kmtool.abc.com节点。


由于此内部xmlns没有NamespaceContext,因此可以使用:

/SOAP-ENV:Envelope/SOAP-ENV:Body/*[local-name()='invokerules'] /*[local-name()='arg0']/text()


然后,您需要将此值加载到另一个InputSource中。
可以通过以下路径访问PartyId属性:


kbdInitiateRequest/kmTestHeader/TestParty/ConductorParty/Party/@PartyID

(因为local-name()中没有任何xmlns,所以无需使用CDATA

07-26 01:02