当前位置: 首页 > 面试题库 >

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

张英范
2023-03-14
问题内容

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

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

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: 演示

<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:

<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>

问题答案:

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

  • .find('ns1:return')不起作用,因为:jQuery用作伪选择器。
  • .find('ns1\:return')也不起作用,因为字符串中的单个反斜杠用作转义字符。"ns1\:return"变为"ns1:return"与前一个相等。
  • .find('ns1\\:return') 应该使用。双反斜杠用于转义冒号。

看来最后一种解决方案在IE和Firefox中可以正常工作,但在Opera,Chrome或Safari中则不能。为了获得最大的兼容性,请使用带有和不带有假前缀的jQuery选择器,即。"ns1\\:return, return"而不是平原ns1\\:return

演示: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");

您可能已经注意到,我为一些变量加了美元符号。按照惯例,为引用jQuery对象的变量加前缀美元符号是一种约定,以避免在开发期间/开发后产生混淆。



 类似资料:
  • 我试图使用jaxb解组一个soap响应。 我尝试使用以下代码查找根元素 这是我的样本回复 我能够获取对象,但当我尝试访问中的时,由于前缀 有人能帮我脱身吗? 更新:有人能帮我吗? 我使用stax xml解析器解析为,但我将其中的所有值都视为null。

  • 我需要解析和打印ns4:功能部分。空手道以json格式打印它。我试着参考这个答案。但是,我得到'ERROR:'前缀'xsi'的命名空间没有被声明.'错误,如果使用建议的xPath.即, 这是我的XML:它包含许多具有不同“ns”值的部分,但我在这里给出了一个额外的内容。 这是我使用的xPath; 注意:我将上述xml保存在一个单独的文件test1中。xml。我只是在读它并解析它的值。 这就是我得到

  • 我尝试像这样解析xml: 我尝试解析这个xml: 但是我的jobNode总是空的。如果我把我的xml改成这样,它就可以工作了:

  • 我必须解析一个XML,其中缺少xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"命名空间,所以xml看起来像这样: 但应该是这样的: type属性确保了正确的子类。 解析器是通过一个手写的xsd自动生成的$ 一个简单的解决方案是在输入XML上运行一个字符串替换(type=\\"到xsi: type=\"),但这很难看。有没有更好的解决办法?

  • 为什么using指令在包含在匿名命名空间中时表现得好像出现在全局范围?

  • 问题 你想解析某个XML文档,文档中使用了XML命名空间。 解决方案 考虑下面这个使用了命名空间的文档: <?xml version="1.0" encoding="utf-8"?> <top> <author>David Beazley</author> <content> <html xmlns="http://www.w3.org/1999/xhtml">