Apologies if this is simple to some, but it's been doing my head in.
I've got my client application receiving the XML from the server but I'm having a hell of a time parsing it.
The XML is :
<?xml version="1.0" encoding="UTF-8"?>
<page>
<id>1</id>
<code>HOME</code>
<template>1</template>
<menulevel>0</menulevel>
<menuname></menuname>
<menutooltip></menutooltip>
<elements>
<element id="content1">
<id>1</id>
<page>1</page>
<container>content1</container>
<type>javascript</type>
<value></value>
<data><![CDATA[showmainmenu()]]></data>
</element>
<element id="content2">
<id>2</id>
<page>1</page>
<container>content2</container>
<type>text</type>
<value></value>
<data><![CDATA[<span style="font-family:helvetica,arial,sans-serif; font-size:10px; text-align:center;">Information goes here.</span>]]></data>
</element>
<element id="title">
<id>3</id>
<page>1</page>
<container>title</container>
<type>text</type>
<value></value>
<data><![CDATA[Economic Dashboard & Summary]]></data>
</element>
</elements>
<generated>Wed, 12 May 2010 14:08:30 +0200</generated>
</page>
What I want to do is to be able, with Javascript, to quickly pick out various bits of data from this information, such as the <data> tag of the <element> that has attribute "title".
So far as I can tell, there's no way of getting an element by its attribute name without traversing the entire DOM tree. Is that right? Seems like a monumental pain in the ass if it's the case, so I'm hoping I'm wrong.
Since I couldn't find a way to do it, I've resorted to the following, but still have issues with my solution :
data=http.responseXML.documentElement
elements=data.getElementsByTagName('element');
var num_elements = elements.length
for (i = 0; i > num_elements ; i++) {
if (elements[i].attributes.getNamedItem('id').value=="title") {
alert(elements[i].getElementsByTagName('data')[0].value
}
}
but that just returns "undefined".
I'd rather not have to traverse the DOM tree with a loop just to find the right node though - there must be a way to reference a node some other way? I can't do it by index number because they won't always be in the same order...
Any insight into either problem would be helpful, thank you in advance.
N.