猿问

无法获取编码的 XML 字符串的值

我有一个从端点返回的长 xml 字符串。


String responseXml = " <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

    <s:Header>

        <ActivityId xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics" CorrelationId="b0cfeb30-eb31-4ff8-91d6-c9f34d69497b">b177c70e-4ab6-448b-a0e6-42478e796167</ActivityId>

    </s:Header>

    <s:Body>

        <IssueDomesticOrderResponse xmlns="http://tempuri.org/">

            <IssueDomesticOrderResult>&lt;Envelope XMLVersion="02" Direction="Result"&gt;&#xD; &lt;Task Code="IssueDomesticOrder"&gt;&#xD; &lt;Success&gt;true&lt;/Success&gt;&#xD; &lt;DateTime&gt;2018-10-05T07:24:27.035983Z&lt;/DateTime&gt;&#xD; &lt;Item&gt;&#xD; &lt;File&gt;&amp;lt;Orders Version="4.00"&amp;gt;&amp;lt;Order No="0NGAOR18100000000603" ProductCd="OR"

                ServiceAgreementCd="0"&amp;gt;&amp;lt;Event StateCd="CC" LCLDT="2018-10-05T08:24:27.4891095+01:00" OfficeCd="LOS001" EventOriginCd="S"&amp;gt;&amp;lt;PurchaseInformation Value="30" CurrencyCd="NGN" ValidUntil="2018-10-05" FeeValue="100.000" FeeCurrencyCd="NGN" PayOfficeCode="LOS001" PayOfficeName="---TEST OFFICE----" PurchaseOfficeCode="LOS001" PurchaseOfficeName="---TEST

                OFFICE----"&amp;gt;&amp;lt;Sender CustomerID="4224" Title="MR" Last="Choji" First="Shikamaru" PostCode="LOS001" City="Lausanne" CountryCd="NG" Mobile="08124533711" /&amp;gt;&amp;lt;Recipient CustomerID="4225" Title="MS" Last="Jira" First="Amy" PostCode="1200" City="Geneve" CountryCd="NG" Mobile="08124577322"


我试图解析 xml 字符串并检索Order No它具有的属性,该属性是Orders其父级的子级,File其父级是Item


我执行了以下操作,但无法检索 Node.jsFile或ItemNode.js 文件。我希望从此 xml 中检索的主要值是 Order 属性No,如果我无法获取 File 节点,则无法获取 Order No 属性。

}


暮色呼如
浏览 141回答 2
2回答

慕尼黑8549860

我有两种方法可以解决您的问题你可以使用任何你想要的方式方法一:Java正确解析xmls听到是一个代码示例,您可以在其中从您的 xml 中获取订单号&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String filepath = "/home/sample.xml"; //this file contains unecaped xml&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder docBuilder = docFactory.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document xmlDoc = docBuilder.parse(filepath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node Order = xmlDoc.getElementsByTagName("Order").item(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NamedNodeMap attr = Order.getAttributes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node no = attr.getNamedItem("No");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Order no : " + no.getNodeValue());此代码的输出订单号:0NGAOR18100000000603这样你就可以对 xmls 进行转义String unescapedStr=StringEscapeUtils.unescapeXml(StringEscapeUtils.unescapeXml(str)));//you can store it in a file if you want我已经完成了 unescapeXml twise方法二:如果您不想格式化 xml 并希望从中获得订单号 使用 Regex 有一种简单的方法可以实现它private static final Pattern TAG_REGEX = Pattern.compile("Order No(.+?) ");&nbsp; &nbsp; private static List<String> getOrderNo(final String str) {&nbsp; &nbsp; &nbsp; &nbsp; final List<String> tagValues = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; final Matcher matcher = TAG_REGEX.matcher(str);&nbsp; &nbsp; &nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagValues.add(matcher.group(1));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return tagValues;&nbsp; &nbsp; }以这种方式调用方法System.out.println(Arrays.toString(getOrderNo(str).toArray()));输出我们会[="0NGAOR18100000000603"]由于您使用了正则表达式,您可以使用字符串替换删除不需要的括号,或者您可以使用仍然有效的正则表达式希望这对你有帮助!!!!

慕尼黑5688855

首先尝试从 SOAP 消息中提取消息正文。然后运行您的代码以对其进行转义并处理其内容。// create message factoryMessageFactory mf = MessageFactory.newInstance();// headers for a SOAP messageMimeHeaders header = new MimeHeaders();&nbsp; &nbsp; &nbsp;header.addHeader("Content-Type", "text/xml");InputStream stream = new ByteArrayInputStream(responseXml .getBytes(StandardCharsets.UTF_8));// create the SOAPMessageSOAPMessage soapMessage = mf.createMessage(header,stream);// get the bodySOAPBody soapBody = soapMessage.getSOAPBody();
随时随地看视频慕课网APP

相关分类

Java
我要回答