js使用遞迴解析xml,js遞迴xml

來源:互聯網
上載者:User

js使用遞迴解析xml,js遞迴xml

xml結構:

複製代碼 代碼如下:
<RightMenuItems>
  <Item Code="New" Name="建立" GroupCode="Edit" GroupName="編輯"/>
  <Item Code="Open" Name="開啟" GroupCode="Edit" GroupName="編輯">
    <item Code="Word" Name="Word文檔" GroupCode="CommonDocument" GroupName="常規"/>
    <item Code="Excel" Name="Excel文檔" GroupCode="CommonDocument" GroupName="常規"/>
    <item Code="CustomDocument" Name="自訂文檔" GroupCode="CustomDocument" GroupName="自訂"/>
  </Item>
  <Item Code="Save" Name="儲存" GroupCode="Edit" GroupName="編輯"/>
  <Item Code="Exit" Name="離開" GroupCode="Exit" GroupName="離開"/>
</RightMenuItems>

解析方法:

複製代碼 代碼如下:
$(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的全部代碼了,超級簡潔,非常使用,給需要的小夥伴參考下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.