tags:

views:

741

answers:

4

Is it possibble to map the structure of an xml file with jquery? After an ajax call the client gets an xml file, but it doesn't know the file's node structure, how could we reach all of its content? Thanks

edited: For examlpe this xml has changing node structure. How could I reconstruct the exact node structure with jquery?

<?xml version="1.0" encoding="utf-8" ?>
<children>
    <child>
     <name>Daniel</name>
     <age>5</age>
     <eye>brown</eye>
    </child>
    <child>
     <name>Herold</name>
     <mother>Helena</mother>
     <hobby>painting<hobby>
    </child>
    <child>
     <name>Katalin</name>
     <birthday>2006-05-26</birthday>
    </child>
</children>
A: 

Um.... well if you've got the XML, you've got the node structure. If you really need to map out the XML before you start working with it, then you would probably loop through all the nodes and map it however you see fit.

Unless I'm not understanding your question properly.

gargantaun
How could I loop through all the nodes?Thats exactly my question is.How could I get all the node's names and values?
danovics
A: 

link , did you mean to parse an xml ?

http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

Haim Evgi
+1  A: 

If this is your XML ('Data.xml'):

<?xml version="1.0" encoding="utf-8" ?>
<Urls>
 <url>
   <name>google</name>
   <link>www.google.com</link>
 </url>
 <url>
   <name>Blah</name>
   <link>http://www.blah.com&lt;/link&gt;
 </url>
</Urls>

You can parse it like so:

   $(document).ready(function() {
       $.get('Data.xml', function(xml) {
           $(xml).find('url').each(function() {
               alert($(this).find('link').text());
               alert($(this).find('name').text());
           })
       });
   });
karim79
A: 
jQuery(function($) {
var discover = function(node) {
    switch (node.nodeType) {
        case node.ELEMENT_NODE:
            var s="<"+node.nodeName;
            for(var i=0;i<node.attributes.length;i++) { 
                var a=node.attributes[i];
                s+=" "+a.nodeName+'="'+a.nodeValue+'"';
            }
            s+=">";
            console.log(s);
            break;
        case node.TEXT_NODE:
            console.log(node.nodeValue);
            break;
    }
    $(node).contents().each(function(i){discover(this);});
    switch (node.nodeType) {
        case node.ELEMENT_NODE:
            console.log("</"+node.nodeName+">");
            break;
    }
}
$.get("ismeretlen.xml",null,discover,"xml");
});

Just for another searchers. I've got this answer on another forum.

danovics