gpt4 book ai didi

java - 如何删除soap header 和附加标签以检索所需的xml?

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:10 27 4
gpt4 key购买 nike

这是我的字符串格式的 xml。

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:processRequestResponse xmlns:ns="http://service.soap.oneflexi.com"><ns:return>
<?xml version="1.0" encoding="UTF-8"?>
<ItemRs language="SG" currency="SGD">
<Items>
<Item>
<CategoryCode />
<CategoryDescription />
<ItemCode>356</ItemCode>
<ItemDescription>20% offer, Latest model</ItemDescription>
<Quantity />
<UnitPrice>24560</UnitPrice>
<ItemBigImagesURL>http://goo.gl/klCGG4</ItemBigImagesURL>
<ItemContent>Front Loading Washing Machine 6.5Kg Capacity 6 Motion Direct Drive</ItemContent>
<ErrorCode />
<ErrorMessage />
<Beaconid>2499</Beaconid>
</Item>
</Items>
<MID />
</ItemRs>
</ns:return>
</ns:processRequestResponse>
</soapenv:Body>
</soapenv:Envelope>

我需要的输出应该是上面 xml 的子集,删除了 SOAP 头和其他标签。我怎样才能在java中实现这个输出?

<ItemRs language="SG" currency="SGD"> 
<Items>
<Item>
<CategoryCode />
<CategoryDescription />
<ItemCode>356</ItemCode>
<ItemDescription>20% offer, Latest model</ItemDescription>
<Quantity />
<UnitPrice>24560</UnitPrice>
<ItemBigImagesURL>http://goo.gl/klCGG4</ItemBigImagesURL>
<ItemContent>Front Loading Washing Machine 6.5Kg Capacity 6 Motion Direct Drive</ItemContent>
<ErrorCode />
<ErrorMessage />
<Beaconid>2499</Beaconid>
</Item>
</Items>
<MID />
</ItemRs>

请提供任何帮助,我们将不胜感激,并提前致谢。

我尝试使用此线程中给出的示例:[1]: Java How to extract a complete XML block

这是java代码

public static void main(String... args)
throws Exception
{
// String xml = "<A><B><id>0</id></B><B><id>1</id></B></A>";
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.soap.oneflexi.com\"><soapenv:Header/><soapenv:Body><ser:processRequest><ser:in0><![CDATA[<BconRq language=\"SG\" currency=\"SGD\"><RqHeader><Date>20140614</Date><Time>162944</Time><TimeZone>GMT+06:00</TimeZone><MessageType>5320</MessageType><VersionNo>9005001</VersionNo><AppVersion>5.0.1</AppVersion><MerchantId>2003001</MerchantId><TerminalId>2004002</TerminalId><StanNo/><LastTxnRefNo/><OTP/></RqHeader><OfferMerchant/><OfferFlag>Y</OfferFlag><BconId>1234</BconId><Items><Item><ItemCode>VC01</ItemCode></Item></Items></BconRq>]]></ser:in0><ser:in4>?</ser:in4></ser:processRequest></soapenv:Body></soapenv:Envelope>";

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));

XPath xPath = XPathFactory.newInstance().newXPath();
Node result = (Node)xPath.evaluate(" Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/BconRs[language=\"SG\"]", doc, XPathConstants.NODE);

// Node result = (Node)xPath.evaluate("A/B[id = '1']", doc, XPathConstants.NODE);
// Soapenv:Envelope/Soapenv:Body/ns:processRequestResponse/ns:return/BconRs[language="SG"]
System.out.println(nodeToString(result));
}

private static String nodeToString(Node node)
throws TransformerException
{
StringWriter buf = new StringWriter();
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xform.transform(new DOMSource(node), new StreamResult(buf));
return(buf.toString());
}

我可能认为 XPATH 遍历到节点 ItemRs 时可能出现错误。

最佳答案

正如您所怀疑的,XPath 看起来不正确。您可能想尝试这个简单的 XPath :

//ItemRs[@language='SG']

要在 XPath 中引用属性,您需要在属性名称的开头使用 @。我尽量避免在 XPath 中提及 namespace 前缀,因为通常我们需要以某种方式注册 namespace 前缀到 namespace URI 的映射(我不确定如何在 Java 中执行此操作,我更喜欢 .NET)。

关于java - 如何删除soap header 和附加标签以检索所需的xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032814/

27 4 0