XML Child
I have a simple XML file that is loaded into a script page as shown below. It converts from string to XML without any problem, but what complicates things is the fact that I cannot get to the child child.
I would like to know why my code is not working and what I need to do to get the tag name.
function load_xml() {
$.ajax({
type: "GET",
url: "file.xml",
dataType: "xml",
success: function (xmlData) {
var $first_child = $(xmlData).children()[0];
var first_name = $first_child.nodeName; // returns a proper name of a node
var $second_child = $first_child.children()[0]; // doesn't work
var $second_name = $second_child.nodeName; // returns nothing (don't know why)
},
error: function () {
alert("Could not retrieve XML file.");
}
});
}
+3
Hubert Siwkin
source
to share
1 answer
In your case, $first_child
not a jQuery collection. You need to wrap it up $()
. Here is the revised version.
var first_child = $(xmlData).children()[0]; // [0] actually returns the first "raw" node
var first_name = first_child.nodeName;
var $first_child = $(first_child);
var second_child = $first_child.children()[0];
var second_name = second_child.nodeName;
+1
Constantinius
source
to share