简体   繁体   中英

$.parseXML works in IE but not Firefox or Chrome

I am using $.ajax to return data from the database as a JSON object. One of the fields is valid XML (has been stored in SQL Server in an XML field). When I use $.parseXML to extract the XML data, all is fine in IE (version 9), but no joy in Firefox or Chrome.

I'm thinking I might need to do 2 separate queries, with one just returning the XML data with contentType as text/XML based upon a Google search. Currently the returnType in $.ajax is JSON as is the contentType in the .ashx handler file (yes, it's an ASP application).

Can anybody offer some insight here?

Thanks in advance, David

Sample:

function ParseXMLRecord(record) {
    var xmlDoc = $.parseXML(record);
    var $xml = $(xmlDoc);
    var Questions = $xml.find("Question"); 
    for (var x = 0; x < Questions.length; x++) {
        var test = Questions[x];
        for (var t = 0; t < Questions[x].childNodes.length; t++) {        
            if (Questions[x].childNodes[t].tagName == 'Response') {
                var controlId = '', value = ''; 
                for (var g = 0; g <  Questions[x].childNodes[t].childNodes.length; g++) {

                    var tagname = Questions[x].childNodes[t].childNodes[g].tagName;

                    if (tagname === 'Control') {

fails on firefox -->    controlId = Questions[x].childNodes[t].childNodes[g].text;

                          ...

======================================================================================

Let me ask this is a different way. Here is the XML I need to parse:

<ResponseSet>
  <Question>
    <Text>Laterality</Text>
    <Response>
      <Control>rbLateralityLft</Control>
      <Value>Left</Value>
    </Response>
  </Question>

etc...

based on the above, this line does not work on Firefox:

controlId = Questions[x].childNodes[t].childNodes[g].text; // nor this one--> Questions[x].childNodes[t].childNodes[g].nodeTypedValue

If you are using jquery to parse the XML, then use jquery to read the parsed data too, for example:

var s = "<ResponseSet>  <Question>    <Text>Laterality</Text>    <Response>      <Control>rbLateralityLft</Control>      <Value>Left</Value>    </Response>  </Question><Question>    <Text>Laterality2</Text>    <Response>      <Control>rbLateralityLft2</Control>      <Value>Left2</Value>    </Response>  </Question></ResponseSet>";

var x = $.parseXML(s);
$.each($(x).children().children(), function (idx, data) {
    var questionText = $(data).find("Text").text();
    var control = $(data).find("Control").text();
    var value = $(data).find("Value").text();
    console.log(questionText + " - " + control + " - " + value);
});

http://jsfiddle.net/TNLer/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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