简体   繁体   English

模拟用于XML读取的jQuery $ .post / $ .ajax

[英]Simulating jQuery $.post / $.ajax for XML reading

Possibly odd question; 可能是奇怪的问题; I'm trying to load an XML into jQuery to traverse it. 我正在尝试将XML加载到jQuery中以遍历它。

Using $.post I can do this perfectly, and specify XML as the dataType. 使用$ .post我可以完美地完成这项工作,并将XML指定为dataType。 My question revolves around how I can get jQuery to use that dataType to understand the same data if it is already in the page, ie I have it in a variable. 我的问题围绕如何让jQuery使用该dataType来理解相同的数据(如果它已经在页面中),即我将它放在一个变量中。

Whenever I use exactly the same XML data in a variable it can't traverse it properly. 每当我在变量中使用完全相同的XML数据时,它就无法正确遍历它。

I've tried taking out the declaration and removing question marks, escaping quotes etc. 我已经尝试取出声明并删除问号,转义引号等。

I've tried loading like:- 我试过加载像: -

var xml = new XML('<blah><moo>134</moo></blah>');

and of course 而且当然

var xml = $('<blah><moo>134</moo></blah>');

and

var xml = '<blah><moo>134</moo></blah>';

and

var xml = "<blah><moo>134</moo></blah>";

etc. What am I doing wrong? 我做错了什么?

The problem is that jQuery doesn't parse XML, except using the built-in browser responseXML property of XMLHttpRequest when doing Ajax requests. 问题是jQuery不解析XML,除了在执行Ajax请求时使用XMLHttpRequest的内置浏览器responseXML属性。 If you pass an XML string to $() , it simply assumes it's HTML, assigns it as the innerHTML of an HTML element and reads the children of that HTML element. 如果将XML字符串传递给$() ,它只是假定它是HTML,将其指定为HTML元素的innerHTML并读取该HTML元素的子元素。 This is not XML parsing. 这不是XML解析。

To parse XML, you can use a function like the following: 要解析XML,您可以使用如下函数:

var parseXml;

if (window.DOMParser) {
    parseXml = function(xmlStr) {
        return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    parseXml = function() { return null; }
}

var xml = parseXml("<blah><moo>134</moo></blah>");
if (xml) {
    window.alert(xml.documentElement.nodeName);
}

UPDATE UPDATE

jQuery 1.5's new parseXML() method does exactly this, and seems to work well. jQuery 1.5的新parseXML()方法正是如此,并且似乎运行良好。

var xml = $.parseXML("<blah><moo>134</moo></blah>");

This gives you an XML document, which you can then traverse using jQuery in the usual way: 这为您提供了一个XML文档,然后您可以通过常规方式使用jQuery遍历:

var $xml = $(xml);
alert($xml.find("moo:first")[0].nodeName);

Here is a sample that uses $.ajax and works cross browser (well Chrome, IE8, FireFox 3.6): 这是一个使用$ .ajax并跨浏览器工作的示例(Chrome,IE8,FireFox 3.6):

var pathToXML = "http://www.site.com/data/data.xml";
var xml;
    $.ajax({type: "GET", 
            url: pathToXML,
            cache: false, 
            dataType: ($.browser.msie) ? "text" : "xml",
            error: function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus)},
            success: function(data){
            // workaround for msie
            if (typeof data == "string") {
                    xml = new ActiveXObject("Microsoft.XMLDOM");
                    xml.async = false; xml.loadXML(data);
            } else { xml = data; }
            xml.setProperty("SelectionLanguage", "XPath");
            // end workaround
            // use $(xml).find('node').text();
            var bobsNodeValue = $(xml).find("node[id='bob']").text();
    }});

This also sets it up as actual XML, which jQuery can traverse like a normal DOM tree using .find() and .text() . 这也将它设置为实际的XML,jQuery可以使用.find().text()像普通的DOM树一样遍历。

EDIT:: Just read through the new comments to the question. 编辑::只需阅读问题的新评论。 Can you post an example of the failed xml here? 你能在这里发布一个失败的xml的例子吗? Also, using .html() will probably result in errors whereas .text() should return a blank string for empty nodes. 此外,使用.html()可能会导致错误,而.text()应返回空节点的空字符串。

EDIT EDIT:: Here is a fiddle showing no errors, what code are you using to traverse and is it all browsers or just a particular one? 编辑编辑::这是一个小提示 ,没有显示任何错误,您使用什么代码进行遍历,是所有浏览器还是只是一个特定的?

您尝试在将xml发送到jQuery之前对其进行urlencode?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM