![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - XML Data by Name instead of childNode Array? [Thread Next] Re: XML Data by Name instead of childNode Array?To: NULL Date: 11/4/2006 10:47:00 AM
<richard.morey@g...> wrote in message
news:1162616042.940967.29340@b......
> 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
>
Use XPath to retrieve nodes:
var resultNodes = xmlDoc.selectNodes("/*/RESULT/text()");
alert(resultNodes[0].nodeValue);
For cross browser version try a ready built library such as zXml,
http://www.nczonline.net/downloads/, or Sarissa.
--
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
