Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
<A>
<z id="232" zzz="3" aaa="13" >
<z id="232" zzz="3" aaa="13">
<z id="232" zzz="3" aaa="13">
</A>


I need to select all 'z' nodes using jquery.
I already know implemntation using Xpath(supported by FF,chrome) and Msxml2.DOMDocument(selectNodes--used in IE)

I basically want to remove check for Browser type and write a common Jquery implementation to do that. I have to put all the nodes seperately into an js array for later processing.
Thanks in Adance


Below 2 lines gets me all the 'z' nodes. Assume result contains above xml.
C#
xmlDoc = $.parseXML(result);
var Nodenew = $(xmlDoc).find("z");


But i see that it is not keeping the order within attributes. For ex: First 'z' nodes has attribute in this order: id,zzz,aaa but i am not getting this order, instead i get zzz,aaa,id.
Is there any method without XPath which can order it and fits for both IE and FF.Thanks
Posted
Updated 21-Mar-13 8:28am
v2

1 solution

Hello,

Below is a small demo HTML. It shows how to process XML using a mix of JQuery & JavaScript.
HTML
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<p id="someElement"></p>
<p id="anotherElement"></p>
<script>
var xml = '<a><z id="232" zzz="3" aaa="13" /><z id="232" zzz="3" aaa="13" /><z id="232" zzz="3" aaa="13" /></a>';
var xmlDoc = $.parseXML( xml );
var znodes = $("z", xmlDoc);
$.each(znodes, function(index, value) {
    printInfo(value);
});

//traverseDOM(xmlDoc, printInfo);

function printInfo(node) {
    $("#someElement").append("NodeType " + node.nodeType + "<br>");
    $("#someElement").append("NodeName " + node.nodeName + "<br>");
    $("#someElement").append("NodeValue " + node.nodeValue + "<br>");
};

function traverseDOM(node, fn){
    fn(node); // execute passed function on current node
    node = node.firstChild; // get node's child
    while (node){ // if child exists
        traverseDOM(node, fn); // recursively call the passed function on it
        node = node.nextSibling; // set node to its next sibling
    }
}
</script>
</body>
</html></br></br></br>

Regards,
 
Share this answer
 
Comments
devcode007 21-Mar-13 15:18pm    
This also works for me. Another question that i updated was about keeping the same order.
Does it ensure same order of nodes('z') as given in passed xml and also the order within attributes(order within attributes is not much of a concern but since i am loading a tree , order of nodes does matter. Using XPATH there is a way using XPathResult.ORDERED_NODE_ITERATOR_TYPE to keep the node order, do we have something in query too to enforce the order.
Prasad Khandekar 21-Mar-13 15:53pm    
I have not tested that but it should. Try playing with sample.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900