使用命名空间解析XML JQuery Ajax响应

时间:2022-10-08 21:25:23

I'm executing a web service call using JQuery and it's ajax function and I'm unable to parse the data coming back. When I alert the data (alert($(data).find("return").text()) its empty. I see the server responding with xml data as outlined below and when I alert(data) I get [object XMLDocument]. Is txt = $(data).find("return").text() valid given my XML structure with a namespace below? I can see the full xml string in firebug. Any ideas?

我正在使用JQuery执行Web服务调用,它是ajax函数,我无法解析返回的数据。当我提醒数据时(alert($(data).find(“return”)。text())为空。我看到服务器响应xml数据,如下所述,当我发出警报(数据)时,我得到[object XMLDocument是.txt = $(data).find(“return”)。text()在我的XML结构下面有一个名称空间有效吗?我可以在firebug中看到完整的xml字符串。有什么想法吗?

var txt = $(data).find("ns1\:return").text(); works on Chrome and Firefox, but not Safari

var txt = $(data).find(“ns1 \:return”)。text();适用于Chrome和Firefox,但不适用于Safari

index.js:

index.js:

$(function () {
$.ajax({
                url: url,
                success: function (data) {
                    var ndx = 0,
                        row,
                        **txt = $(data).find("return").text(),**
                        xml = unescape(txt),
                        xmlDoc = $.parseXML(xml),
                        firstrow = $(xmlDoc).find(
                                "results").children(":first");

                    // populate the table based on the results returned by
                    // the web service
                    $("table.results thead").empty();
                    $("table.results tbody").empty();
                    row = $("<tr/>");

                    row.append($("<th/>").text("#").addClass("ndx"));
                    firstrow.children().each(function () {
                        row.append($("<th/>").text(this.nodeName));
                    });
                    row.appendTo($("table.results thead"));

                    $(xmlDoc).find("row").each(function () {
                        row = $("<tr/>");
                        row.append($("<td/>").text(ndx + 1).addClass("ndx"));
                        $(this).children().each(function () {
                            row.append($("<td/>").text($(this).text()));
                        });
                        row.appendTo($("table.results tbody"));
                        ndx++;
                    });

                    // clear the table if no results were returned
                    if (ndx == 0) {
                        // no rows returned
                        $("table.results thead").empty();
                        $("table.results tbody").empty();
                    }

                    statusNotice("Records Returned: " + ndx);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    // display the error returned by the web service
                    var xmlDoc = $(XMLHttpRequest.responseXML);
                    statusError(xmlDoc.find("Text").text());
                },          
                complete: function(XMLHttpRequest, textStatus) {
                    // hide the busy dialog
                    $("#busy-dlg").dialog("close");
                }
            });
       });

index.html: Demo

index.html:演示

<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-min.js"></script>
<script type="text/javascript" src="js/jquery.layout-latest.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</head>
<body>
//table displaying results from ajax call here
</body>
</html>

XML:

XML:

<ns1:executeResponse xmlns:ns1="http://sqlws.test.com">
<ns1:return>
    <results>
        <row>
            <attribute1>value1</attribute1>
            <attribute2>value2</attribute2>
        </row>
        <row>
            <attribute1>value1</attribute1>
            <attribute2>value2</attribute2>
        </row>
    </results>
</ns1:return>
</ns1:executeResponse> 

1 个解决方案

#1


9  

When an element is prefixed by a namespace, you have to also add the namespace:

当元素以名称空间为前缀时,您还必须添加名称空间:

  • .find('ns1:return') does not work, because : is used by jQuery as pseudo-selectors.
  • .find('ns1:return')不起作用,因为:jQuery使用它作为伪选择器。
  • .find('ns1\:return') does not work either, because a single backslash in a string is used as an escape character. "ns1\:return" becomes "ns1:return" which is equal to the previous one.
  • .find('ns1 \:return')也不起作用,因为字符串中的单个反斜杠用作转义字符。 “ns1 \:return”变为“ns1:return”,它等于前一个。
  • .find('ns1\\:return') should be used. The double backslash is used to escape the colon.
  • 应该使用.find('ns1 \\:return')。双反斜杠用于逃避冒号。

It appears that the last solution works fine in IE and Firefox, but not Opera, Chrome or Safari. To get maximum compatibility, use jQuery selectors with, and without fake prefix, ie. "ns1\\:return, return" instead of a plain ns1\\:return.

似乎最后的解决方案在IE和Firefox中运行良好,但不适用于Opera,Chrome或Safari。要获得最大的兼容性,请使用带有伪前缀的jQuery选择器,即。 “ns1 \\:return,return”而不是普通的ns1 \\:return。

Demo: http://jsfiddle.net/5BQjv/51/

演示:http://jsfiddle.net/5BQjv/51/

// For example, this is the result:
var data = '<ns1:executeResponse xmlns:ns1="http://sqlws.test.com">' +
               '<ns1:return>' + 
                   '<results> <row> ... </row> </results>' +
               '</ns1:return>' +
           '</ns1:executeResponse>';

// The very first thing is to parse the string as XML. NOT later!
var $xmlDoc = $($.parseXML(data));

// Then, look for the element with the namespace:
var $txt = $xmlDoc.find('ns1\\:return, return');

// No need to use unescape or something, just use DOM manipulation:
// `results` is the immediate child. Don't use .find, but .children
var $firstrow = $txt.children("results").children(":first");

As you may have noticed, I have prefixed some variables with a dollar sign. It's the convention to prefix variables which refer to jQuery objects with a dollar sign, to avoid confusion during/after development.

你可能已经注意到了,我用一个美元符号作为前缀。为变量引用带有美元符号的jQuery对象的前缀是常规,以避免在开发期间/之后出现混淆。

#1


9  

When an element is prefixed by a namespace, you have to also add the namespace:

当元素以名称空间为前缀时,您还必须添加名称空间:

  • .find('ns1:return') does not work, because : is used by jQuery as pseudo-selectors.
  • .find('ns1:return')不起作用,因为:jQuery使用它作为伪选择器。
  • .find('ns1\:return') does not work either, because a single backslash in a string is used as an escape character. "ns1\:return" becomes "ns1:return" which is equal to the previous one.
  • .find('ns1 \:return')也不起作用,因为字符串中的单个反斜杠用作转义字符。 “ns1 \:return”变为“ns1:return”,它等于前一个。
  • .find('ns1\\:return') should be used. The double backslash is used to escape the colon.
  • 应该使用.find('ns1 \\:return')。双反斜杠用于逃避冒号。

It appears that the last solution works fine in IE and Firefox, but not Opera, Chrome or Safari. To get maximum compatibility, use jQuery selectors with, and without fake prefix, ie. "ns1\\:return, return" instead of a plain ns1\\:return.

似乎最后的解决方案在IE和Firefox中运行良好,但不适用于Opera,Chrome或Safari。要获得最大的兼容性,请使用带有伪前缀的jQuery选择器,即。 “ns1 \\:return,return”而不是普通的ns1 \\:return。

Demo: http://jsfiddle.net/5BQjv/51/

演示:http://jsfiddle.net/5BQjv/51/

// For example, this is the result:
var data = '<ns1:executeResponse xmlns:ns1="http://sqlws.test.com">' +
               '<ns1:return>' + 
                   '<results> <row> ... </row> </results>' +
               '</ns1:return>' +
           '</ns1:executeResponse>';

// The very first thing is to parse the string as XML. NOT later!
var $xmlDoc = $($.parseXML(data));

// Then, look for the element with the namespace:
var $txt = $xmlDoc.find('ns1\\:return, return');

// No need to use unescape or something, just use DOM manipulation:
// `results` is the immediate child. Don't use .find, but .children
var $firstrow = $txt.children("results").children(":first");

As you may have noticed, I have prefixed some variables with a dollar sign. It's the convention to prefix variables which refer to jQuery objects with a dollar sign, to avoid confusion during/after development.

你可能已经注意到了,我用一个美元符号作为前缀。为变量引用带有美元符号的jQuery对象的前缀是常规,以避免在开发期间/之后出现混淆。