在Firefox中使用jQuery解析XML的问题。

时间:2022-12-01 13:10:54

I'm using JavaScript/jQuery trying to parse an XML document that's been fetched via an AJAX Get request. I can't post the XML document, but it is well-formed and the tags that I'm looking for are present. The AJAX request is successful and returns the full XML document in both Chrome and Firefox--now I just can't parse it, for some reason. The code is working in Chrome but not Firefox or IE. I'm not really interested in IE right now, but I'm lost as to why it isn't working in Firefox.

我正在使用JavaScript/jQuery来解析通过AJAX Get请求获取的XML文档。我不能发布XML文档,但它格式良好,而且我正在寻找的标记已经存在。AJAX请求成功,返回了Chrome和Firefox中的完整XML文档——现在由于某种原因,我无法解析它。代码在Chrome中运行,而不是Firefox或IE。我现在对IE并不是很感兴趣,但是我对它为什么不能在Firefox中工作失去了兴趣。

JavaScript:

JavaScript:

window.onload = function getGroupSets() {
$.ajax( {
    type: "GET",
    dataType: "application/xml",
    url: '../api/indicatorGroupSets.xml',
    success: function( xml ) {

        /*Find Indicator Group Set tags in XML file */

        alert($(xml).find('indicatorGroupSet').length);
        //This alert reads '0' in Firefox, '4' in Chrome
        //The following function is not executed in Firefox:

        $( xml ).find( 'indicatorGroupSet' ).each( function( ) {

            /*For each Set, get the name & ID*/

            var groupSet = {
                name: $( this ).attr( 'name' ),
                id: $( this ).attr( 'id' )
            };
            alert(groupSet.name);
            //Nothing displays here
        } );
    },
    error: function( jqXHR, textStatus, errorThrown ) {

        /*If an error occurs, alert the issue and a message to users */

        alert( jqXHR + ':' + textStatus + ':' + errorThrown + '\nThere has been an error, please refresh the page or contact the site administrator if the problem persists.' );
    }
} );
}

I've been looking for solutions for most of the day, but everything seems to be about how a parser isn't working in IE, not much on this issue in Firefox.

我今天大部分时间都在寻找解决方案,但似乎所有的问题都是关于解析器在IE中不能工作的原因,而在Firefox中没有太多的问题。

I appreciate any input!

我很欣赏任何输入!

1 个解决方案

#1


0  

Try telling jQuery that you are loading HTML instead. It is my experience that jQuery can't traverse XML in Firefox. It is possible that some unknown, specific conditions trigger this behaviour.

试着告诉jQuery你正在加载HTML。我的经验是jQuery不能在Firefox中遍历XML。可能是某些未知的、特定的条件触发了这种行为。

$.get("example.xml", function(data) {
    ....
}, "html");

It is necessary to traverse the document using the localName property, if your document contains namespaces:

如果文档包含名称空间,则需要使用localName属性遍历文档:

if (this.localName === "c:tagname") {
    ....
}

It is possible to navigate the document using normal jQuery functionality, if it doesn't contain namespaces:

如果文档不包含名称空间,那么可以使用普通的jQuery功能导航文档:

if ($(this).is("tagname")) {
    ....
}

Far from pretty, but gets the job done. In Firefox, at least. :)

远非漂亮,但能完成任务。至少,在Firefox。:)

#1


0  

Try telling jQuery that you are loading HTML instead. It is my experience that jQuery can't traverse XML in Firefox. It is possible that some unknown, specific conditions trigger this behaviour.

试着告诉jQuery你正在加载HTML。我的经验是jQuery不能在Firefox中遍历XML。可能是某些未知的、特定的条件触发了这种行为。

$.get("example.xml", function(data) {
    ....
}, "html");

It is necessary to traverse the document using the localName property, if your document contains namespaces:

如果文档包含名称空间,则需要使用localName属性遍历文档:

if (this.localName === "c:tagname") {
    ....
}

It is possible to navigate the document using normal jQuery functionality, if it doesn't contain namespaces:

如果文档不包含名称空间,那么可以使用普通的jQuery功能导航文档:

if ($(this).is("tagname")) {
    ....
}

Far from pretty, but gets the job done. In Firefox, at least. :)

远非漂亮,但能完成任务。至少,在Firefox。:)