wessleym Posted January 22, 2010 Share Posted January 22, 2010 This should be simple, but I'm having some issues. If I have the following XML data: <node id="containerNode"> <node>One Node</node> <node>Another Node</node> </node> and I ask for xmlhttp.responseXML.getElementsByTagName("span")[0].firstChild.data Firefox returns "undefined." And I guess I understand why. But what if I need all XML within containerNode? Is there any way to pull it out? Thank you! Link to comment Share on other sites More sharing options...
0 +XamlMonkey Subscriber² Posted January 23, 2010 Subscriber² Share Posted January 23, 2010 (edited) May I ask why you used "span" in the getElementsByTagName function. In any case try using nodeValue instead of data. Some example: XML Data: <images> <image> <title>Image 1</title> <url>http://someurl/image1</url> </image> <image> <title>Image 2</title> <url>http://someurl/image2</url> </image> <image> <title>Image 3</title> <url>http://someurl/image3</url> </image> </images> JavaScript: var docElement = xmlhttp.responseXML.documentElement; var imgNodes = docElement.getElementsByTagName("image"); // Get information of each image for(var i = 0; i < imgNodes.length; i++) { // Get image title var imageTitle = imgNodes[i].childNodes[1].firstChild.nodeValue; // Get image url var imageUrl = imgNodes[i].childNodes[3].firstChild.nodeValue; alert(imageTitle + " - " + imageUrl); } Hope this helps. Link to comment Share on other sites More sharing options...
0 wessleym Posted January 23, 2010 Author Share Posted January 23, 2010 Thank you, but I was actually trying to do something a little different. Using your example, what if I wanted everything within the <images> node in text form? <images> <image> <title>Image 1</title> <url>http://someurl/image1</url> </image> <image> <title>Image 2</title> <url>http://someurl/image2</url> </image> <image> <title>Image 3</title> <url>http://someurl/image3</url> </image> </images> Link to comment Share on other sites More sharing options...
0 +XamlMonkey Subscriber² Posted January 24, 2010 Subscriber² Share Posted January 24, 2010 Ok, I don't think this is quite what you want, but have a look at it. This will return all the text, but strips any tags. https://developer.mozilla.org/En/DOM/Node.textContent Note: IE uses text instead of textContent. Link to comment Share on other sites More sharing options...
0 wessleym Posted January 24, 2010 Author Share Posted January 24, 2010 Right, and in this case, the tags are vital. Link to comment Share on other sites More sharing options...
0 +XamlMonkey Subscriber² Posted January 24, 2010 Subscriber² Share Posted January 24, 2010 Well, I don't think there is a standardized DOM function that will do that for the XML DOM. If it was HTML, you could have used innerHTML, but this doesn't work on the XML DOM unfortunately. Firefox has something called XMLSerializer. Google Chrome also implemented it, but IE doesn't have anything similar. Example: function getNodesAsText(objXML, nodeName) { var docElement = objXML.documentElement; var allNodes = docElement.getElementsByTagName(nodeName); //Initialize new XMLSerializer var xmlSer = new XMLSerializer(); var nodesText = ""; // Convert each node into text for(var i = 0; i < allNodes.length; i++) { nodesText += xmlSer.serializeToString(allNodes[i]); } return nodesText; } Call: getNodesAsText(xmlhttp.responseXML,"image") The above call will return all the image tags from my XML data example. Link to comment Share on other sites More sharing options...
0 wessleym Posted January 27, 2010 Author Share Posted January 27, 2010 Thanks again, TheWiseIstari. Does anybody know a standards-complaint method, or does it not exist? Link to comment Share on other sites More sharing options...
Question
wessleym
This should be simple, but I'm having some issues. If I have the following XML data:
and I ask for
xmlhttp.responseXML.getElementsByTagName("span")[0].firstChild.dataFirefox returns "undefined." And I guess I understand why. But what if I need all XML within containerNode? Is there any way to pull it out? Thank you!
Link to comment
Share on other sites
6 answers to this question
Recommended Posts