Layered xml for unordered list in jquery

I am pulling my hair trying to create an unordered list from an xml file with no luck. I know how to handle xml from jQuery, but I can't figure out how to make a layered unordered list.

Here is what I have achieved so far.

Xml file

<?xml version="1.0" encoding="utf-8"?>
<Parent>Director
    <Children>Exe Director1</Children>
    <Children>Exe Director2</Children>
    <Parent>Exe Director2
        <Children>Sub Director 1</Children>
        <Children>Sub Director 2</Children>
        <Parent>Sub Director 3
            <Children>Cameraman 1</Children>
            <Children>Cameraman 2</Children>
        </Parent>
    </Parent>    
</Parent>

Html file

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    var levels;
    $(document).ready(function() 
    {
        $.ajax({
            type: "GET",
            url: "test.xml",
            dataType: "xml",
            success: xmlParser
        });
    });
    function xmlParser(xml)
{
    $(xml).find("Children").each(function()
    { 
        var text = $(this).text();
    });
}
</script>
</head>
<body>
    <div id="ListContainer"></div>
</body>
</html>

This is the expected list.

<ul>
    <li>Exe Director1</li>
    <li>Exe Director2</li>
    <ul>
        <li>Sub Director 1</li>
        <li>Sub Director 2</li>
        <ul>
            <li>Cameraman 1</li>
            <li>Cameraman 2</li>
        </ul>
    </ul>
</ul>

Can you guys help me!

Edit:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    var levels;
    $(document).ready(function() 
    {
        $.ajax({
            type: "GET",
            url: "test.xml",
            dataType: "xml",
            success: function (xml) { xmlParser($(xml)); }
        });
    });
    function xmlParser(xml) {
        //alert($(xml).contents());  
        var $ul = $("<ul>"); // For each Parent, create an <ul>
        $(xml).contents().each(function (i, el) {
            if (el.nodeType == 3) return true;
            if (el.nodeName.toUpperCase() == "CHILDREN") 
            {
               $("<li>").text($(el).text()).appendTo($ul); // Append <li> Children
            } else 
            {
               $ul.append(xmlParser($(el))); // Recursively append the other Parent
            }
            //$("#ListContainer").append($ul);
        });
        //alert($ul.html());
        $("#ListContainer").append($ul);
    }
</script>
</head>
<body>
    <div id="ListContainer"></div>
</body>
</html>
+5
source share
2 answers

Recursively build an unordered list:

function xmlParser($xml) {   
    var $ul = $("<ul>"); // For each Parent, create an <ul>
    $xml.contents().each(function (i, el) {
        if (el.nodeType == 3) return true;
        if (el.nodeName.toUpperCase() == "CHILDREN") {
           $("<li>").text($(el).text()).appendTo($ul); // Append <li> Children
        } else {
           $ul.append(xmlParser($(el))); // Recursively append the other Parent
        }
    });
    return $ul;
}

Here is the demo . Think of each group and the corresponding as a separate block. For example, if your XML looked like this: ParentChildren

<Parent>Director
    <Children>Exe Director1</Children>
    <Children>Exe Director2</Children>
</Parent>

The result is HTML:

<ul>
    <li>Exe Director1</li>
    <li>Exe Director2</li>
</ul>

? : <ul>, Children <li>. , , Parent, <ul> , , <ul>.

, , , , jQuery , success:

success: function (xml) {
  xmlParser($(xml));
}
+5

success: xmlParser,

success: function(data){
    xmlParser(data)
}
+2

All Articles