dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: IE problem - I need help with xmlhttprequest and parsing xml with javascript.

  1. #1
    Join Date
    Oct 2004
    Posts
    3

    Question IE problem - I need help with xmlhttprequest and parsing xml with javascript.

    Hello.

    What i'm trying to do is load a simple xml/rss -feed by using XMLHttprequest and the access the object and write out the content.


    It now works in Firefox but for some reasong it doesn't work in IE though it should. It gives no errors or anything - it just doesn't anything in IE. I know this technique works in IE but i'm doing something wrong.

    Any help with that??

    *****************
    Below is the script code.
    *****************
    <script type="text/javascript">
    <!--

    var debugCounter = 0;
    var xhr;
    var xmlobject;
    var resp;

    // global flag
    var isIE = false;

    function debugMsg(msg) {
    debugCounter++;
    var t = document.createTextNode("\n" + debugCounter + ": " + msg);
    var br = document.createElement("br");
    var debug = document.getElementById("xmlInfo");
    debug.appendChild(t);
    debug.appendChild(br);
    }

    function getDataViaHTTP(url)
    {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = processReqChange;
    xhr.open("GET", url, true);
    xhr.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject || typeof ActiveXObject == "object" || typeof ActiveXObject == "function")
    //else if (window.ActiveXObject || typeof ActiveXObject == "object" || typeof ActiveXObject == "function")
    {
    isIE = true;
    xhr = new ActiveXObject("MsXml2.XmlHttp");/*Msxml2.XMLHTTP*/
    if (xhr) {
    xhr.onreadystatechange = processReqChange;
    xhr.open("GET", url, true);
    xhr.send();
    //writeXMLData();

    //;
    }
    }

    }

    // handle onreadystatechange event of xhr object
    function processReqChange() {
    // only if req shows "loaded"
    if (xhr.readyState == 4) {
    // only if "OK"
    //if (xhr.status == 200) {


    //resp = xhr.responseText;
    xmlobject = xhr.responseXML;

    /*if ( resp.length < 0 ) {
    debugMsg("Response too short");
    return;
    }

    var rawLines = resp.split("\n");
    if ( typeof rawLines != "object" ) {
    debugMsg("Bad Response (it may be empty)");
    return;
    }*/

    writeXMLData();

    //} else {
    // alert("There was a problem retrieving the XML data:\n"/* + xhr.statusText*/);
    //}
    }
    }

    // retrieve text of an XML document element, including
    // elements using namespaces
    function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
    // IE/Windows way of handling namespaces
    result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
    // the namespace versions of this method
    // (getElementsByTagNameNS()) operate
    // differently in Safari and Mozilla, but both
    // return value with just local name, provided
    // there aren't conflicts with non-namespace element
    // names
    result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
    // get text, accounting for possible
    // whitespace (carriage return) text nodes
    if (result.childNodes.length > 1) {
    return result.childNodes[1].nodeValue;
    } else {
    return result.firstChild.nodeValue;
    }
    } else {
    return "n/a";
    }
    }



    var e = document.getElementById("xmlInfo");


    function writeXMLData()
    {
    //alert(xmlobject[0]);
    e.innerHTML = "";
    var items = xhr.responseXML.getElementsByTagName("item");
    var titles = xhr.responseXML.getElementsByTagName("title");
    var links = xhr.responseXML.getElementsByTagName("link");
    var dates = xhr.responseXML.getElementsByTagName("pubDate");
    var descriptions = xhr.responseXML.getElementsByTagName("description");
    var authors = xhr.responseXML.getElementsByTagName("author");

    for(var i = 0; i < items.length; ++i)

    {

    linkki = getElementTextNS("texta", "link", items[i], 0);
    otsikko = getElementTextNS("textb", "title", items[i], 0);
    aika = getElementTextNS("textc", "pubDate", items[i], 0);
    sender = getElementTextNS("textd", "author", items[i], 0);

    e.appendChild(document.createElement("div")).innerHTML = '<a href="'+linkki+'">'+otsikko+'</a><br>'+sender+' - '+aika+'';
    //alert(linkki);

    //e.style.fontWeight = "bold";
    }


    }




    ************
    Sample of the XML -file
    ************


    <?xml version="1.0"?>
    <rss version="2.0">
    <channel>
    <item>

    <textb:title>Fw: Salzburg calling</textb:title>

    <texta:link>http://www.domain.com/</texta:link>
    <texte:description>-</texte:description>
    <textd:author>

    Elina

    </textd:author>

    <textcubDate>2004-10-13 19:43</textcubDate>
    </item>

    </channel>
    </rss>
    Last edited by supperhead; 10-20-2004 at 10:58 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Derby, England
    Posts
    297
    I know this is late, and you have probably solved it but:-


    function getDataViaHTTP(url)
    {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = processReqChange;
    xhr.open("GET", url, true);
    xhr.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject)
    {
    isIE = true;
    req = new ActiveXObject("Msxml2.XMLHTTP");
    if (xhr) <---xhr is not a defined variable in this branch. Do you mean req
    {
    xhr.onreadystatechange = processReqChange;
    xhr.open("GET", url, true);
    xhr.send();
    }
    }

    }


  3. #3
    Join Date
    Oct 2004
    Posts
    3
    Thank you for the reply. Indeed that was the firsthing I corrected on the script. Thats what you get when staying up all night

    BUT it didn't fix it. Now IE does the xmlhttprequest as it should. This can be tested with the xhr.responseText - it returns the content of the file as text. But i can't access the 'nodes' in the xmlobject.

    I've updated the current code above - it works on Mozilla/Firefox but it doesn't work in IE though it should.

    Any ideas would be welcome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles