Problem:
Parsing XML
I'm currently working on a pretty straight-forward AJAX application. In this application, I get some XML, and then parse it. An example of what I'm working with is as such:
<?xml version="1.0"?> <postRoot xml:lang="EN"> <post type="comment" date="January 1, 1970"> <user>athlon32</user> <content>Hello World</content> </post> </postRoot>
XMLHttpRequest
, my trouble is with parsing the results. I've tried tons of things, but I just can't seem to get the different child nodes.Now, let's say we have something like this:
var xml = xhr.responseXML; var allPosts = xml.getElementsByTagName('post');
childNode
to access user
& content
? And if so, how? I've tried many things, but nothing is working :/ Is there a better way to parse the results I get back from the server?Anyways, thanks in advance. I'm sure this is really simple, but I'm a C coder just trying Javascript for the first time, so I'm kinda lost.
[Re]
Solve:
This should like this
<script type="text/javascript"> if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","test.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; myXml = xmlDoc.documentElement.getElementsByTagName("post"); var userValue = myXml[0].getElementsByTagName("user")[0].childNodes[0].nodeValue; var contentValue = myXml[0].getElementsByTagName("content")[0].childNodes[0].nodeValue; alert(userValue +","+ contentValue); </script>
[Re]
Wow Thanks.