Ameer A. Lawi
Ameer A. Lawi

Reputation: 83

get and parsing xml using ajax on IE

I have the following XML returned from a URL:

    <?xml version='1.0' encoding='UTF-8' ?>
<!--  search hits from ThunderBallWebService -->
<iHS>
  <Params>
    <param name='start'><![CDATA[0]]></param>
    <param name='return'><![CDATA[content_arabic;url;metatag.publishdt;metatag.title;metatag.site;metatag.channel;metatag.image;metatag.newImage;metatag.newstopics]]></param>
    <param name='groupby'><![CDATA[metatag.site]]></param>
    <param name='output'><![CDATA[xml]]></param>
    <param name='hitsPerPage'><![CDATA[12]]></param>
    <param name='all'><![CDATA[مصر]]></param>
    <param name='filter'><![CDATA[-id:*learning.aljazeera*]]></param>
  </Params>
  <Response>
    <Qtime>16</Qtime>
  </Response>
  <HitsCount>
    <hitsPerPage>12</hitsPerPage>
    <total>1</total>
    <matches>7</matches>
    <start>0</start>
    <end>1</end>
    <totalIsExact>true</totalIsExact>
  </HitsCount>
  <Results>
    <group id='0' name='metatag.site' value='57258a24e91a42b0b2399dccf49cf8c1' count='7' start='0'>
      <Hits>
        <Hit id='0'>
          <metatag.publishdt type=''><![CDATA[2015-11-02]]></metatag.publishdt>
          <metatag.image type=''><![CDATA[65cb6546-b449-4c72-b0c0-eeb07d9adfb1]]></metatag.image>
          <metatag.title type=''><![CDATA[انطلاق انتخابات <em>مصر</em> بالداخل والمعارضة تقاطع]]></metatag.title>
          <id type=''><![CDATA[http://site.aja.qa/news/pages/91385ee9-203d-4969-b5d9-19083fae1bf5]]></id>
          <url type=''><![CDATA[http://site.aja.qa/news/pages/91385ee9-203d-4969-b5d9-19083fae1bf5]]></url>
          <content_arabic type=''><![CDATA[انطلاق انتخابات <em>مصر</em> بالداخل والمعارضة تقاطع تنطلق اليوم الأحد المرحلة الأولى من الانتخابات]]></content_arabic>
          <metatag.site type=''><![CDATA[57258A24E91A42B0B2399DCCF49CF8C1]]></metatag.site>
          <metatag.newstopics type=''><![CDATA[42c25b5e-f336-47c0-a1f8-d17deec868d7,]]></metatag.newstopics>
          <metatag.channel type=''><![CDATA[84AD340701C24F699597D698D2547472]]></metatag.channel>
        </Hit>
        <Hit id='1'>
          <metatag.publishdt type=''><![CDATA[2015-10-19]]></metatag.publishdt>
          <metatag.image type=''><![CDATA[e5762dbc-705d-43d2-920e-9cbb89e5a850]]></metatag.image>
          <metatag.title type=''><![CDATA[كوميديا الدعاية الانتخابية بمصر]]></metatag.title>
          <id type=''><![CDATA[http://site.aja.qa/news/arabic/2015/10/19/%d9%83%d9%88%d9%85%d9%8a%d8%af%d9%8a%d8%a7-%d8%a7%d9%84%d8%af%d8%b9%d8%a7%d9%8a%d8%a9-%d8%a7%d9%84%d8%a7%d9%86%d8%aa%d8%ae%d8%a7%d8%a8%d9%8a%d8%a9-%d8%a8%d9%85%d8%b5%d8%b1]]></id>
          <url type=''><![CDATA[http://site.aja.qa/news/arabic/2015/10/19/%d9%83%d9%88%d9%85%d9%8a%d8%af%d9%8a%d8%a7-%d8%a7%d9%84%d8%af%d8%b9%d8%a7%d9%8a%d8%a9-%d8%a7%d9%84%d8%a7%d9%86%d8%aa%d8%ae%d8%a7%d8%a8%d9%8a%d8%a9-%d8%a8%d9%85%d8%b5%d8%b1]]></url>
          <content_arabic type=''><![CDATA[كوميديا الدعاية الانتخابية بمصر تبدو مشاهد الدعاية الانتخابية لمرشحى برلمان <em>مصر</em>، كما لو كانت مقتطعة]]></content_arabic>
          <metatag.site type=''><![CDATA[57258A24E91A42B0B2399DCCF49CF8C1]]></metatag.site>
          <metatag.newstopics type=''><![CDATA[33a9ad72-a45f-4827-bb18-2e71685abf89,e8b8f3f7-4fd6-4d78-846d-9c8d2239fda4,]]></metatag.newstopics>
          <metatag.channel type=''><![CDATA[84AD340701C24F699597D698D2547472]]></metatag.channel>
        </Hit>
        <Hit id='2'>
          <metatag.publishdt type=''><![CDATA[2015-11-18]]></metatag.publishdt>
          <metatag.image type=''><![CDATA[246d383c-bb2a-4167-b75f-0bf8c68b0ca8]]></metatag.image>
          <metatag.title type=''><![CDATA[إندبندنت: فيديو منسوب لتنظيم الدولة يهدد إسرائيل]]></metatag.title>
          <id type=''><![CDATA[http://site.aja.qa/news/arabic/2015/10/24/%d8%a5%d9%86%d8%af%d8%a8%d9%86%d8%af%d9%86%d8%aa-%d9%81%d9%8a%d8%af%d9%8a%d9%88-%d9%85%d9%86%d8%b3%d9%88%d8%a8-%d9%84%d8%aa%d9%86%d8%b8%d9%8a%d9%85-%d8%a7%d9%84%d8%af%d9%88%d9%84%d8%a9-%d9%8a%d9%87%d8%af%d8%af-%d8%a5%d8%b3%d8%b1%d8%a7%d8%a6%d9%8a%d9%84]]></id>
          <url type=''><![CDATA[http://site.aja.qa/news/arabic/2015/10/24/%d8%a5%d9%86%d8%af%d8%a8%d9%86%d8%af%d9%86%d8%aa-%d9%81%d9%8a%d8%af%d9%8a%d9%88-%d9%85%d9%86%d8%b3%d9%88%d8%a8-%d9%84%d8%aa%d9%86%d8%b8%d9%8a%d9%85-%d8%a7%d9%84%d8%af%d9%88%d9%84%d8%a9-%d9%8a%d9%87%d8%af%d8%af-%d8%a5%d8%b3%d8%b1%d8%a7%d8%a6%d9%8a%d9%84]]></url>
          <content_arabic type=''><![CDATA[ الجهات، من جانب شبه جزيرة سيناء في <em>مصر</em> ومن جانب دمشق في سوريا ومن جهات أخرى. تهديد مباشر وأضافت الصحيفة]]></content_arabic>
          <metatag.site type=''><![CDATA[57258A24E91A42B0B2399DCCF49CF8C1]]></metatag.site>
          <metatag.newstopics type=''><![CDATA[42c25b5e-f336-47c0-a1f8-d17deec868d7,]]></metatag.newstopics>
          <metatag.channel type=''><![CDATA[84AD340701C24F699597D698D2547472]]></metatag.channel>
        </Hit>
      </Hits>
    </group>
  </Results>
  <Faceted>
    <field name='metatag.site'>
      <value name='57258a24e91a42b0b2399dccf49cf8c1' count='7'>57258a24e91a42b0b2399dccf49cf8c1 (7)</value>
      <value name='null' count='0'>null (0)</value>
    </field>
  </Faceted>
</iHS>

when I try to parse it in Chrome and Firefox, everything works like a charm, but in IE i'm getting the following error:

XML5632: Only one root element is allowed. Line: 2, Column 1

here is my AJAX :

    function ajxGetXml(url) {
    debugger;
    $.ajax({
        crossDomain: true,
        type: "GET",
        cache: true,
        url: url,// decodeURI(url), //encodeURI(url),
        dataType: "xml",
        success: parseXml,
        timeout: 10000,        // sets timeout for the request (10 seconds)
        error: function (req, err) { console.log('my message' + err); }
    });
}

and the function to parse the XML :

    function parseXml(xObj) {
    debugger;
    var dom = null;
    if (window.DOMParser) {
        try {
            dom = (new DOMParser()).parseFromString(xObj, "text/xml");
        }
        catch (e) { dom = null; }
    }
    else if (window.ActiveXObject) {
        try {
            dom = new ActiveXObject('Microsoft.XMLDOM');
            dom.async = false;
            if (!dom.loadXML(xObj)) // parse error ..
                window.alert(dom.parseError.reason + dom.parseError.srcText);
        }
        catch (e) { dom = null; }
    }
}

I'm stuck here for 4 days with no luck, any help will be appreciated.

Upvotes: 1

Views: 138

Answers (2)

Ameer A. Lawi
Ameer A. Lawi

Reputation: 83

i found the solution!, my problem was with encoding and decoding the url in ajax, its work on both way on chrome and FF but IE requires the url to be Decoded! and i don't know the reason.

Upvotes: 0

J. V. A.
J. V. A.

Reputation: 529

I agree, given that line# in the error message:

XML5632: Only one root element is allowed. Line: 2, Column 1

it's likely the comment. But...

According to the XML standard ( https://www.w3.org/TR/2006/REC-xml-20060816/ ) a comment is valid there:

document   ::=   prolog element Misc*

prolog     ::=      XMLDecl? Misc* (doctypedecl Misc*)?

XMLDecl    ::=      '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'

Misc       ::=      Comment | PI | S

and xmllint passes your file. But IE is, well IE...

If nothing else, I suppose there is always a brute force:

xObj.replace(/<!--(.*?)-->/, "");

Upvotes: 1

Related Questions