Click to See Complete Forum and Search --> : [RESOLVED] Parsing an Actual XML Document via Ajax


phpnovice
04-19-2006, 09:05 AM
I have a recursive function that I'm starting off like this:

list = xmlhttp.responseXML.documentElement;
if((items = list.childNodes).length > 0) {
list = document.createElement("UL");
list.id = type+"List";
BuildCategoryItems(list, items, type);
obj.appendChild(list);
}

Then, the function is as follows:

function BuildCategoryItems(list, items, type) {
var sub, subitm, obj, x, len = items.length;
for(x=0; x<len; ++x) {
obj = list.appendChild(document.createElement("LI"));
obj.CCid = items[x].getAttribute('Id');
obj.CCtype = type;
obj.onclick = HilightListItem;
obj.appendChild(document.createTextNode(items[x].getAttribute('Title')));
if((subitm = items[x].childNodes).length > 0) {
obj.className = "plus";
sub = obj.appendChild(document.createElement("UL"));
sub.style.display = "none";
BuildCategoryItems(sub, subitm, type);
} else {
obj.className = "bullet";
}
}
}

The purpose, of which, is to transform an XML document into an HTML Unordered List with up to nine levels deep. This is working in IE, but is failing in FF at the method shown in red. Sorry, but I'm completely new to parsing XML documents and I don't know what I should be using to retrieve node attributes. Help?

TIA

Kor
04-19-2006, 09:40 AM
Am I wrong, or in XML attributes must be always in lowercase?

phpnovice
04-19-2006, 12:25 PM
I've been waiting for someone to bring up this issue -- even though, to me, it is a bit of a side issue at this point. I asked my client for all lower-case node names and attribute names. However, my client says that standards dictate that the recommended XML capitalization is to use word caps (or whatever these are called) -- FirstName, LastName, etc. I was hoping somebody might know better. :rolleyes: My client was also the one that said standards dictate that a particular XML node name may not be nested within itslef. I posted that question in another thread in this forum, but only got one reply, from NogDog, which said he thought this was OK, too. ;)

In the meantime, I still need to know what method to use in Firefox for retrieving the XML attributes of a node. Again, though it works perfectly in IE, Firefox reports an error something like this:

items[x].getAttribute is not a function.

Kor
04-19-2006, 01:05 PM
you may try element.getAttributeNode()

phpnovice
04-19-2006, 07:35 PM
That didn't work. That returns an attribute object -- and my attribute value was still not there. At any rate...

I solved it. Using the Venkman JavaScript Debugger, I found that Firefox was also creating text nodes for the carriage-return/line-feeds in the data. :rolleyes: So, coding the function this way solved the problem -- because there is no text is this XML data (only attributes and their values):
function BuildCategoryItems(list, items, type) {
var sub, subitm, obj, x, len = items.length;
for(x=0; x<len; ++x) {
if(items[x].nodeName != "#text") {
obj = list.appendChild(document.createElement("LI"));
obj.CCid = items[x].getAttribute('Id');
obj.CCtype = type;
obj.onclick = HilightListItem;
obj.appendChild(document.createTextNode(items[x].getAttribute('Title')));
if((subitm = items[x].childNodes).length > 0) {
obj.className = "plus";
sub = obj.appendChild(document.createElement("UL"));
sub.style.display = "none";
BuildCategoryItems(sub, subitm, type);
} else {
obj.className = "bullet";
}
}
}
}

Kor
04-20-2006, 01:03 AM
That didn't work. That returns an attribute object -- and my attribute value was still not there. At any rate...


I forgot to tell you, I thought you were more close with XML DOM.

element.getAttributeNode('some_attribute').nodeValue

phpnovice
04-20-2006, 09:04 AM
I thought you were more close with XML DOM.
Thanks for that, but I did say:
...I'm completely new to parsing XML documents...
No matter, though... The problem is resolved.

Cheers.