【问题标题】:How to add commas to output retrieved in XML.text?如何在 XML.text 中检索到的输出中添加逗号?
【发布时间】:2018-08-26 21:44:29
【问题描述】:

目前我正在使用 SOAP 来检索我的值。所以现在的问题是

当我收到响应时,它是 XML 格式的,所以为了查看响应,我做了一个 response.responseText

所以代码看起来像这样:

$.ajax(settings).complete(function (response) {
     console.log(response.responseText);
}

我在 console.log 中得到的响应是:

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <ns:findResponse xmlns:ns="http://property.property.ws" xmlns:ax221="http://property.property.ws/xsd" xmlns:ax223="http://filter.common.nyx.nl/xsd">
            <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">48</ns:return>
            <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">50</ns:return>
            <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">51</ns:return>
        </ns:findResponse>
    </soapenv:Body>
</soapenv:Envelope>

如果你向右滚动,你可以看到我的三个值 48 50 和 51。

所以我想提取这 3 个值。截至目前,我正在这样做:

var xmlDoc = $.parseXML(response.responseText);     
var $xml = $(xmlDoc);

var primaryKeysOfProperties = $xml.text();

console.log(primaryKeysOfProperties);               

我从 primaryKeysOfProperties 得到的结果是 485051。

期望的输出: 我想让输出变成这样的 48,50,51。

我已经在这里工作了 2 天.. 我研究并发现了一些类似的堆栈溢出问题,但这些问题有点不同。我试图改变,但我失败了。任何人都可以帮忙吗? :)

【问题讨论】:

    标签: javascript xml soap


    【解决方案1】:

    试试这个,result 数组应该包含您要查找的内容

    const xml = `<?xml version='1.0' encoding='UTF-8'?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header/>
        <soapenv:Body>
            <ns:findResponse xmlns:ns="http://property.property.ws" xmlns:ax221="http://property.property.ws/xsd" xmlns:ax223="http://filter.common.nyx.nl/xsd">
                <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">48</ns:return>
                <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">50</ns:return>
                <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">51</ns:return>
            </ns:findResponse>
        </soapenv:Body>
    </soapenv:Envelope>`
    
    const xmlDoc = $.parseXML(xml);
    const $xml = $(xmlDoc);
    const result = [];
    $xml.find('ns\\:return').each(function(){
      result.push($(this).text());
    });
    
    console.log(result.join(','))
    

    【讨论】:

    • 我得到的响应是空白的。我试图 console.log (el) 我得到 [object Object]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多