gpt4 book ai didi

java - XPATH 不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:36 31 4
gpt4 key购买 nike

我正在尝试使用 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 block 内,因此您需要在访问其中的数据之前重新解析 s1:arg0 的值。

您需要分两步完成此操作

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

由于您没有此内部 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(),因为 CDATA 中没有任何 xmlns)

关于java - XPATH 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21115321/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com