ucantblamem
asked on
XmlHttpRequest In Javascript - not finding nodeValue's ???
I am playing around with XmlHttpRequest to add some nifty functionality to a CMS. My problem is that, using DOM, I CAN access the object.nodeName, but not the object.nodeValue. My first assumption was that my XML was not correctly formed, however I have checked a million times and it's fine. Here is what I'm working with:
var req;
function loadXMLDoc(url) {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.X MLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
else {
// do nothing
}
}
function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
clearFileList();
buildFileList();
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
}
// empty Topics select list content
function clearFileList() {
document.getElementById('f ileList'). innerHTML = '';
}
function appendToList(divi, value, content) {
var opt;
opt = divi;
opt.innerHTML = (opt.innerHTML + content);
}
function buildFileList() {
var listdiv = document.getElementById("f ileList");
var items = req.responseXML.getElement sByTagName ("file");
// loop through <file> elements, and add each nested
// <title> element to List
for (var i = 0; i < items.length; i++) {
// Finds TITLE element and outputs the value.
appendToList(listdiv, i, ('<li class="attFile" id="att'+ i +'"><a href="#">' + items[i].firstChild.nodeVa lue + '</a></li>'));
}
}
My XML file outputs this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kcatt>
<response>1</response>
<err></err>
<filelist>
<file>
<title>Happy Lappy</title>
<path>050513-095240|||mums _new_lappy .jpg</path >
<type>image</type>
<description>Mums' great new laptop.</description>
</file>
<file>
<title>image</title>
<path>050427-031554|||1ser iesjune200 301.jpg</p ath>
<type>image</type>
<description>is this an image! i think it is!</description>
</file>
</filelist>
</kcatt>
I've tried using getElementsByTagName() to find the "title" element, all sorts of crazy hair-brained stuff, but it just keeps outputing "undefined".
Any ideas? Can you see any errors in my code? Your help is most appreciated!
var req;
function loadXMLDoc(url) {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.X
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
else {
// do nothing
}
}
function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
clearFileList();
buildFileList();
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
}
// empty Topics select list content
function clearFileList() {
document.getElementById('f
}
function appendToList(divi, value, content) {
var opt;
opt = divi;
opt.innerHTML = (opt.innerHTML + content);
}
function buildFileList() {
var listdiv = document.getElementById("f
var items = req.responseXML.getElement
// loop through <file> elements, and add each nested
// <title> element to List
for (var i = 0; i < items.length; i++) {
// Finds TITLE element and outputs the value.
appendToList(listdiv, i, ('<li class="attFile" id="att'+ i +'"><a href="#">' + items[i].firstChild.nodeVa
}
}
My XML file outputs this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<kcatt>
<response>1</response>
<err></err>
<filelist>
<file>
<title>Happy Lappy</title>
<path>050513-095240|||mums
<type>image</type>
<description>Mums' great new laptop.</description>
</file>
<file>
<title>image</title>
<path>050427-031554|||1ser
<type>image</type>
<description>is this an image! i think it is!</description>
</file>
</filelist>
</kcatt>
I've tried using getElementsByTagName() to find the "title" element, all sorts of crazy hair-brained stuff, but it just keeps outputing "undefined".
Any ideas? Can you see any errors in my code? Your help is most appreciated!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It's funny, cause that just doesn't seem logical, but it works??? Thanks heaps, you've saved me alot of headache!