欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js使用遞歸解析xml

 更新時(shí)間:2014年12月12日 10:24:31   投稿:hebedich  
這篇文章主要介紹了js使用遞歸解析xml,需要的朋友可以參考下

xml結(jié)構(gòu):

復(fù)制代碼 代碼如下:

<RightMenuItems>
  <Item Code="New" Name="新建" GroupCode="Edit" GroupName="編輯"/>
  <Item Code="Open" Name="打開" GroupCode="Edit" GroupName="編輯">
    <item Code="Word" Name="Word文檔" GroupCode="CommonDocument" GroupName="常規(guī)"/>
    <item Code="Excel" Name="Excel文檔" GroupCode="CommonDocument" GroupName="常規(guī)"/>
    <item Code="CustomDocument" Name="自定義文檔" GroupCode="CustomDocument" GroupName="自定義"/>
  </Item>
  <Item Code="Save" Name="保存" GroupCode="Edit" GroupName="編輯"/>
  <Item Code="Exit" Name="離開" GroupCode="Exit" GroupName="離開"/>
</RightMenuItems>

解析方法:

復(fù)制代碼 代碼如下:

$(xml).find("RightMenuItems").each(function () {
   this.data = Traversal($(this).children());
 });
var Traversal = function (nodes) {
        var itemList = new Array();
        $.each(nodes, function () {
            var entity = new RightMenuEntity();
            var obj = $(this);
            entity.Code = obj[0].getAttribute("Code");
            entity.Name = obj[0].getAttribute("Name");
            entity.GroupCode = obj[0].getAttribute("GroupCode");
            entity.GroupName = obj[0].getAttribute("GroupName");
            if (obj[0].hasChildNodes()) entity.ChildItems = Traversal(obj.children());
            itemList.push(entity);
        });
        return itemList;
    };

以上就是javascript使用遞歸解析XML的全部代碼了,超級(jí)簡(jiǎn)潔,非常使用,給需要的小伙伴參考下。

相關(guān)文章

最新評(píng)論