为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

暮色呼如 2021-10-27 19:07:23
我有一个从端点返回的长 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 属性。}
查看完整描述

2 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

我有两种方法可以解决您的问题


你可以使用任何你想要的方式


方法一:


Java正确解析xmls


听到是一个代码示例,您可以在其中从您的 xml 中获取订单号


            String filepath = "/home/sample.xml"; //this file contains unecaped xml 

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            Document xmlDoc = docBuilder.parse(filepath);

            Node Order = xmlDoc.getElementsByTagName("Order").item(0);

            NamedNodeMap attr = Order.getAttributes();

            Node no = attr.getNamedItem("No");

            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(.+?) ");


    private static List<String> getOrderNo(final String str) {

        final List<String> tagValues = new ArrayList<String>();

        final Matcher matcher = TAG_REGEX.matcher(str);

        while (matcher.find()) {

            tagValues.add(matcher.group(1));

        }

        return tagValues;

    }

以这种方式调用方法


System.out.println(Arrays.toString(getOrderNo(str).toArray()));

输出我们会


[="0NGAOR18100000000603"]


由于您使用了正则表达式,您可以使用字符串替换删除不需要的括号,或者您可以使用仍然有效的正则表达式


希望这对你有帮助!!!!


查看完整回答
反对 回复 2021-10-27
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

首先尝试从 SOAP 消息中提取消息正文。然后运行您的代码以对其进行转义并处理其内容。


// create message factory

MessageFactory mf = MessageFactory.newInstance();

// headers for a SOAP message

MimeHeaders header = new MimeHeaders();     

header.addHeader("Content-Type", "text/xml");

InputStream stream = new ByteArrayInputStream(responseXml .getBytes(StandardCharsets.UTF_8));

// create the SOAPMessage

SOAPMessage soapMessage = mf.createMessage(header,stream);

// get the body

SOAPBody soapBody = soapMessage.getSOAPBody();


查看完整回答
反对 回复 2021-10-27
  • 2 回答
  • 0 关注
  • 140 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信