473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML Data by Name instead of childNode Array?

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

Nov 4 '06 #1
2 6661
ri***********@gmail.com wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 4 '06 #2
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

Nov 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
1
by: Randy Howard | last post by:
Suppose you want to have a large number of items (as an array of struct) wherein one field is "record-specific" and of variable length, yet not violate standard C (C90 probably, since C99 isn't...
2
by: Bob Jenkins | last post by:
I have this cute data structure that I just found a second opportunity to use. I googled for references to it, but came up empty. I probably just don't know its name. The algorithm on this...
4
by: Derek Hart | last post by:
For testing purposes, I need some code that will loop through all attributes and elements in an XML file, and replace the data with the element or attribute name? Does anybody have code that will...
4
by: Cerebral Believer | last post by:
Hi I need help! Forgive me I am a PHP newbie. I have a small script that enables me to send a form from an HTML page. I want to use the HTML formatted form because the design of my website is...
1
by: captain_gni | last post by:
Hi All, New to asp.net here. Been combing google since monday trying to find the right solution. Most of the solutions are good and reflect the same examples in the .net books I have but xml is...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
7
by: xhunter | last post by:
Hi, I am trying to use this function to get the for data : function getform(divid) { var getstr=''; var obj=document.getElementById(divid)? document.getElementById(divid) :...
0
by: GavReynolds | last post by:
Hi All I am writing a program to load in an xml file from the server and create an animation to represent this data (for use as a 'front end' for the data). I have loaded in the XML data into...
1
by: Van Dugall | last post by:
Hello I have been writing a game in javascript which works fine but I wanted to step it up and request the data of the game from the server. I haven't really used php...all I know is from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.