2011-08-11 45 views
1

我需要技术专业知识才能格式化XML并以html格式呈现。我一直在使用JQuery XML解析器,并且我需要帮助构建html部分。JQuery XML解析器格式帮助

data.xml中

<xml> 
<rs:data> 
<z:row Category="Sales " URLMenu="http://www.abc.com, Sales.com" /> 
<z:row Category="Products" URLMenu="http://www.google.com, Products.com" /> 
<z:row Category="Sales "URLMenu="http://www.abc.com/services, Services.com" /> 
<z:row Category="Products" URLMenu="http://www.citigroup.net, Financial.com" /> 
<z:row Category="Products" SubCategory="International" URLMenu="http://www.google.com,  United States" /> 
<z:row Category="Products" SubCategory="International" URLMenu="http://www.googe.com, Australia" /> 
</rs:data></xml> 

JQuery的功能

<script type="text/javascript"> 
    $(document).ready(function() { 
     var thisHtml = ''; 
     var url = 'xml/Data.xml'; 
     $.get(url, function (d) { 
      $(d).find('z\\:row').each(function() { 
       thisHtml += '<ul>'; 
       { 
        thisHtml += '<li><a href="">' + $(this).attr("Category") + '</a></li>'; 
       } 
       thisHtml += '</ul>'; 
      });    $('bd').append($(thisHtml)); 
     }); 
    }); 
</script> 

下面是HTML代码段需要被动态创建

<ul> 
<li>Sales 
    <ul> 
     <li><a>Sales.com</a></li> 
     <li><a>Products.com</a></li> 

    </ul> 
</li> 
<li>Products 
    <ul> 
     <li><a>Services.com</a></li> 
     <li><a>Financial.com</a></li> 
     <li>International 
      <ul> 
       <li><a>United States</a></li> 
       <li><a>Australia</a></li> 
      </ul> 
     </li> 
    </ul> 
</li>               

所需的HTML将同名的所有类别和URLMenu分组列出。 因为我是JQuery的新手,请你帮我循环和渲染?

感谢

回答

0

如果你有一个固定的类别,你可以使用下面的选择为每个块和顺序渲染项目:

$(d).find('z\\:row[Category="Sales"]').each(...); 
$(d).find('z\\:row[Category="Products"]').each(...); 

否则,我猜你将不得不遍历数据两次,以便在第一次运行中按类别对数据进行分组,并在第二次中打印分组数据。

您可能还想考虑使用模板插件来渲染HTML,例如:http://api.jquery.com/tmpl/