XML Data by Name instead of childNode Array?

R

richard.morey

Hi --

I am trying my first attempt at parsing XML via JavaScript. I currently
have this code:

str =
"<RESPONSE><RESULT>SUCCESS</RESULT><SECURITY>10</SECURITY></RESPONSE>"
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(str);
xmlObj = xmlDoc.documentElement;

I am getting the values for "RESULT" and "SECURITY" as below:


alert('xmlObj.childNodes(0).nodeName =
'+xmlObj.childNodes(0).nodeName)
alert('xmlObj.childNodes(0).text = '+xmlObj.childNodes(0).text)

alert('xmlObj.childNodes(1).nodeName =
'+xmlObj.childNodes(1).nodeName)
alert('xmlObj.childNodes(1).text = '+xmlObj.childNodes(1).text)

but I would like to be able to get the value for each field without
having to iterate through each child. Additionally, I would like my
code to be cross-browser compliant and from what I understand the
"text" property is only available on IE. When I try using "nodeValue"
it is always null.

Any insight would be greatly appreciated!

Rich
 
M

Martin Honnen

I am trying my first attempt at parsing XML via JavaScript. I currently
have this code:

str =
"<RESPONSE><RESULT>SUCCESS</RESULT><SECURITY>10</SECURITY></RESPONSE>"
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(str);




If you know the structure of the XML exactly then all you need is e.g.
var root = xmlDoc.documentElement;
var result = root.getElementsByTagName('RESULT')[0];
var security = root.getElementsByTagName('SECURITY')[0];
if (result != null) {
// now you can access the content for above simple example
// it suffices to do e.g.
alert(result.firstChild.nodeValue);
}
if (security != null) {
alertt(security.firstChild.nodeValue);
}
Depending on which browsers you want to support there are other
solutions, W3C DOM Level 2 implementations (like older Mozillas, like
Opera 8) do not expose a property that concatenates the text contents of
all children and desendants in a element node so for that you would need
to write your own. For Firefox or newer Mozillas and for Opera 9 there
however is the textContent property, for MSXML in IE there is the text
property so restricted to those you could also script e.g.
if (result != null) {
if (typeof result.textContent != 'undefined') {
alert(result.textContent);
}
else if (typef result.text != 'undefined') {
alert(result.text);
}
}

Firefox and Opera 9 also support the W3C DOM Level 3 XPath API.
 
R

richard.morey

If you know the structure of the XML exactly then all you need is e.g.
var root = xmlDoc.documentElement;
var result = root.getElementsByTagName('RESULT')[0];
var security = root.getElementsByTagName('SECURITY')[0];
if (result != null) {
// now you can access the content for above simple example
// it suffices to do e.g.
alert(result.firstChild.nodeValue);
}
if (security != null) {
alertt(security.firstChild.nodeValue);
}

This worked! Thank you so much. This will make my project MUCH easier!

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top