Process Each Node Of XML In JQuery

JQuery is a very useful tool to use to loop through elements, this is because of the each function. It will loop through each of the elements it has in the jQuery object, this can be a HTML dom element, or multiple elements or in this example is xml nodes.

JQuery can load a file of XML and loop through by using 2 built-in jQuery functions the .get function and the each function.

First you use the .get function to get all elements of the XML. Just pass the location of the file as the first parameter then inside this function you can use the xml variable.

$.get("xmlfile.xml", {}, function (xml) {

}

Inside this function you can use the xml variable to process the xml nodes.

Now create a jQuery object search for the name of the node you want 'node' with the XML document and process each one of the nodes.

$('node', xml).each(function (i) {
        //Do something
});

Full Code

//Load xml file
$.get("xmlfile.xml", {}, function (xml) {
     //Loop through each node
     $('node', xml).each(function (i) {
        //Do something
     });
}):

Advertise here 50% Off

Comment