Altova Mailing List Archives


Re: XML Data by Name instead of childNode Array?

From: "Joe Fawcett" <joefawcett@---------.------>
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



Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.