JQuery XML Parser Format帮助

时间:2011-08-11 12:21:35

标签: jquery jquery-xml

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

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的新手,你可以帮我循环和渲染吗?

由于

1 个答案:

答案 0 :(得分:0)

如果您有一组固定的类别,则可以对每个块使用以下选择器并按顺序呈现项目:

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

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

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