Search code examples
jqueryjquery-xml

jquery using xml as html in IE 8.0 and other browsers


I need to get a xml string from the server, which is formatted as:

<xml><material>
<record>
<id>..</id>
<name>..</name>
<quantity>..</quantity>
<plink>..</plink>
</record>
<record>
..
</record>
</xml></material>

Client side get this xml and process it:

$.ajax({
  url:'getrecords.php',
  cache:false,
  type:"GET",
  dataType:'html',
  success:function (html){
    var records=$(html).find("record");
    alert(records.length);
  }

My problem is that, this code is working fine in Chrome and Firefox but not in IE 8.0 (I am using 8.0) It alerts 0 in IE while it alerts the correct length in chrome and firefox.

I try this but it also shows length of records is 0 in IE but fine in Chrome

var records=$("<xml><root><item></item></root></xml>").find("item");

Solution

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="../../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var xml = '<?xml version="1.0" encoding="utf-8" ?><material><record><id>1</id><name>A</name><quantity>10</quantity>'
                        + '<plink>test</plink></record><record><id>2</id><name>B</name><quantity>20</quantity><plink>test1</plink>'
                        + '</record><record><id>3</id><name>C</name><quantity>34</quantity><plink>test2</plink></record><record>'
                        + '<id>4</id><name>B</name><quantity>45</quantity><plink>test6</plink></record></material>';
    
    
    
                //            $.ajax({
                //                url: 'getrecords.php', cache: false, type: "GET", dataType: 'html',
                //                success: function (xml) { 
                var data = $($.parseXML(xml));
                var records = $(data).find('record');
                alert(records.length);
                //                }
                //            });
            });
        </script>
    </head>
    <body>
        <?xml version="1.0" encoding="utf-8" ?>
        <material>
      <record>
        <id>1</id>
        <name>A</name>
        <quantity>10</quantity>
        <plink>test</plink>
      </record>
      <record>
        <id>2</id>
        <name>B</name>
        <quantity>20</quantity>
        <plink>test1</plink>
      </record>
      <record>
        <id>3</id>
        <name>C</name>
        <quantity>34</quantity>
        <plink>test2</plink>
      </record>
      <record>
        <id>4</id>
        <name>B</name>
        <quantity>45</quantity>
        <plink>test6</plink>
      </record>
    </material>
    </body>
    </html>