【问题标题】:How to Parse XML Response Using jQuery如何使用 jQuery 解析 XML 响应
【发布时间】:2011-11-29 22:48:35
【问题描述】:

我正在尝试使用 jQuery 解析 xml 响应并仅在页面中输出一个元素,但我没有成功。

以下是我用于响应和解析它的代码。

$.ajax({
    url: UCMDBServiceUrl,
    type: "POST",
    dataType: "xml",
    data: soapMessage,
    success: UCMDBData,
    crossDomain: true,
    contentType: "text/xml; charset=\"utf-8\""
});
alert("Sent2");
return false;
}

function UCMDBData(xmlHttpRequest, status, msg)
{
     alert("Came back1");
     $(xmlHttpRequest.responseXML).find('tns:CIs').each(function()
     {
        alert("Came back2");
        $(this).find("ns0:CI").each(function()
        {
            alert("Came back3");
            $("#output").append($(this).find("ns0:ID").text());
        });
     });     
}

我收到了关于“Come back1”的警报,但它似乎没有更进一步。下面是我尝试使用上面的 jquery 代码解析的 XML 响应。我最终试图从响应中返回的文本在这个元素中

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header />
    <soapenv:Body>
        <tns:getFilteredCIsByTypeResponse xmlns:ns0="http://schemas.hp.com/ucmdb/1/types" xmlns:ns1="http://schemas.hp.com/ucmdb/ui/1/types" xmlns:ns2="http://schemas.hp.com/ucmdb/1/types/query" xmlns:ns3="http://schemas.hp.com/ucmdb/1/types/props" xmlns:ns4="http://schemas.hp.com/ucmdb/1/types/classmodel" xmlns:ns5="http://schemas.hp.com/ucmdb/1/types/impact" xmlns:ns6="http://schemas.hp.com/ucmdb/1/types/update" xmlns:ns7="http://schemas.hp.com/ucmdb/discovery/1/types" xmlns:ns8="http://schemas.hp.com/ucmdb/1/types/history" xmlns:tns="http://schemas.hp.com/ucmdb/1/params/query">
            <tns:CIs>
                <ns0:CI>
                    <ns0:ID>4d030502995a00afd989d3aeca2c990c</ns0:ID>
                    <ns0:type>nt</ns0:type>
                    <ns0:props>
                        <ns0:strProps>
                            <ns0:strProp>
                                <ns0:name>name</ns0:name>
                                <ns0:value>prodoo</ns0:value>
                            </ns0:strProp>
                        </ns0:strProps>
                        <ns0:booleanProps>
                            <ns0:booleanProp>
                                <ns0:name>host_iscomplete</ns0:name>
                                <ns0:value>false</ns0:value>
                            </ns0:booleanProp>
                        </ns0:booleanProps>
                    </ns0:props>
                </ns0:CI>
            </tns:CIs>
            <tns:chunkInfo>
                <ns0:numberOfChunks>0</ns0:numberOfChunks>
                <ns0:chunksKey>
                    <ns0:key1 />
                    <ns0:key2 />
                </ns0:chunksKey>
            </tns:chunkInfo>
        </tns:getFilteredCIsByTypeResponse>
    </soapenv:Body>
</soapenv:Envelope>

所以我的问题是如何正确解析数据?我相信代码语法是正确的,但我没有得到预期的返回结果。我会很感激任何帮助,谢谢。

编辑

我已将我的代码修改为以下建议,但仍然没有运气:

$.ajax({
    url: UCMDBServiceUrl,
    type: "POST",
    dataType: "xml",
    data: soapMessage,
    success: UCMDBData,
    crossDomain: true,
    contentType: "text/xml;"
    });
alert("Sent2");
return false;
}

function UCMDBData(data, textStatus, jqXHR) {
    alert("Came back1");
    $(data).find('tns:CIs').each(function () {
        alert("Came back2");
        $(this).find("ns0:CI").each(function () {
            alert("Came back3");
            $("#output").append($(this).find("ns0:ID").text());
            document.AppServerForm.outputtext.value = document.AppServerForm.outputtext.value + "http://localhost:8080/ucmdb/cms/directAppletLogin.do?objectId=" + $(this).find('ns0:ID').text() +"&infopane=VISIBLE&navigation=true&cmd=ShowRelatedCIs&interfaceVersion=8.0.0&ApplicationMode=ITU&customerID=1&userName=admin&userPassword=admin";

    });
});

}

当我执行时收到的唯一警报消息是“Come back1”,这意味着代码仍然没有使用 jquery 正确地通过 xml。还有其他建议吗?

【问题讨论】:

  • 您是在尝试匹配 ns0:CI 的单个实例,还是可以多次出现?
  • 只有一个 ns0:CI 实例被返回,正如您在我在这里发布的 XML 响应中看到的那样。

标签: javascript jquery html xml


【解决方案1】:

命名空间范围的名称需要稍微不同的处理。根据这个答案: jQuery XML parsing with namespaces 您需要改用属性选择器 [@nodeName=tns:CIs]。

对于 1.3 之后的 jQuery 版本,您可能需要删除“@”。另一个建议是转义冒号:.find('tns\:CIs'),这是 hacky,因为它将句法前缀与语义命名空间(uri)混为一谈。因此,如果前缀更改,此方法将中断。更正确的答案将识别前缀到命名空间 uri 的映射。 jquery-xmlns plugin for namespace-aware selectors 在这方面看起来很有希望。

【讨论】:

  • 其实我还是有问题,这在 Firefox 中有效,但不是 IE 8(这里使用的是什么)。
  • 带有命名空间前缀的 nodeName 过滤器在 jQuery 1.7 中不再有效。
  • 原来 IE 问题是由于我连接到 WSDL 并在 url 中传递了身份验证。即user:pass@mywebservice.com,为了解决这个问题,我需要在 XML (Soap) 标头中进行简单的身份验证。
  • 作为记录,您使用的是什么版本的 jQuery,解决方案在哪个版本中有效?
【解决方案2】:

您的 jQuery 成功函数格式错误。它必须是格式

function UCMDBData(data, textStatus, jqXHR) {
    alert("Came back1");
    $(data).find('tns:CIs').each(function () {
        alert("Came back2");
        $(this).find("ns0:CI").each(function () {
            alert("Came back3");
            $("#output").append($(this).find("ns0:ID").text());
        });
    });
}

此外,在您的$.ajax 函数中,将contentType 行更改为contentType: "text/xml",而不是之前的(假设您将XML 发送到服务器)。

请参阅jQuery.ajax() documentation 了解更多信息。

【讨论】:

    【解决方案3】:

    根据您的评论,为什么要用 jQuery 做一些疯狂的事情?只需使用 javascript 本身!

    var open = '<ns0:ID>';
    var close = '</ns0:ID>';
    
    var start = obj.indexOf(open) + open.length;
    var end = obj.indexOf(close);
    
    var result = obj.slice(start, end);
    

    这是一个jsfiddle,它显示了它的实际效果。

    【讨论】:

    • 它适用于这个特定的例子,但如果节点有属性就会中断。
    【解决方案4】:

    可能正确的语法是

    success: function(xml) {
        $(xml).find('tns:CIs').each(function() {
        ......
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2019-12-08
      • 2019-08-10
      • 2012-03-26
      相关资源
      最近更新 更多