邮递员:如何在 XML 主机响应中解析 CDATA 中的 xml?

我在这里和其他地方阅读了多种资源,但未能解决我的问题。

我将 XML 格式的 POST 请求发送到具有由另一个 XML“请求”构成的 CDATA 的主机,并且响应具有相同的结构。从 CDATA 内的嵌套 XML 响应中,我需要提取一个属性('ATTRIBUTE7'),它将成为 Postman 中的一个环境变量。我的理解是,由于 CDATA,VASDataString 的内容被视为不应解析的文本块。

我怎样才能超越这个街区?

要求:


<?xml version="1.0" encoding="UTF-8"?>

<va:VASMessage xmlns:va="http://subdomain.domain.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://subdomain.domain.com" file:/C:/work/work-idea/somewhere/vas.xsd"  Version="1.1.2">

    <va:Header>

        <va:HeadElem1>ABC123</va:HeadElem1>

        <va:HeadElem2>192</va:HeadElem2>

        <va:OriginalTime>2020-05-13T17:21:00</va:OriginalTime>

        <va:TransactionCode>70</va:TransactionCode>

        <va:ReqProcessing>0</va:ReqProcessing>

        <va:MessageType>0</va:MessageType>

        <va:MessageDirection>0</va:MessageDirection>

    </va:Header>

    <va:Body>

        <va:VASProviderID>ABC</va:VASProviderID>

        <va:VASProductID>PIPPO</va:VASProductID>

        <va:VASMessageType>PIPPO_REQUEST</va:VASMessageType>

        <va:TransactionTime>2020-05-13T17:21:00</va:TransactionTime>

        <va:RetailerID>0101</va:RetailerID>

        <va:BranchID>01</va:BranchID>

        <va:ShopID>0001</va:ShopID>

        <va:POSID>001</va:POSID>

        <va:VASDataString><![CDATA[

        <HostMessage>

            <PIPPO_REQUEST>

                <HostRequest>

                    <Header Attr1="AB12" Timestamp="{{TimeStamp}}000" />

                    <GORDO Operation="000192" />

                    <Check>

                        <Request Attr2="000100031408" Attr3="8029513427007" Attr4="2500">

                            <ElemX Attr5="29" />

                        </Request>

                    </Check>

                </HostRequest>

            </PIPPO_REQUEST>

        </HostMessage>]]>

        </va:VASDataString>

    </va:Body>

</va:VASMessage>

慕斯王
浏览 87回答 1
1回答

慕田峪9158850

解决方案:const xml2js = require('xml2js');xml2js.parseString(pm.response.text(), (err, result) => {&nbsp; &nbsp; if (err) {&nbsp; &nbsp; &nbsp; &nbsp; console.error(err);&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; const body = result.VASMessage.Body[0].VASDataString[0];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; xml2js.parseString(body, (err, result) => {&nbsp; &nbsp; &nbsp; &nbsp; if (err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.error(err);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; pm.environment.set("ATTRIBUTE7",&nbsp; JSON.stringify(result.HostMessage.PIPPO_RESPONSE[0].HostResponse[0].$.ATTRIBUTE7));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(rresult.HostMessage.PIPPO_RESPONSE[0].HostResponse[0].$.ATTRIBUTE7);&nbsp; &nbsp; })});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript