1

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

数据.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>

查询函数

<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 Snippet

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

谢谢

4

1 回答 1

0

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

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

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

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

于 2011-08-11T12:36:29.620 回答