IE problem - I need help with xmlhttprequest and parsing xml

admin

Administrator
Staff member
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, 0);
otsikko = getElementTextNS("textb", "title", items, 0);
aika = getElementTextNS("textc", "pubDate", items, 0);
sender = getElementTextNS("textd", "author", items, 0);

e.appendChild(document.createElement("div")).innerHTML = '<a href=http://www.webdeveloper.com/forum/archive/index.php/"'+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>

<textc:pubDate>2004-10-13 19:43</textc:pubDate>
</item>

</channel>
</rss>
 
Top