Js uses recursive parsing xml and js recursive xml

Source: Internet
Author: User

Js uses recursive parsing xml and js recursive xml

Xml structure:

Copy codeThe Code is as follows:
<RightMenuItems>
<Item Code = "New" Name = "New" GroupCode = "Edit" GroupName = "Edit"/>
<Item Code = "Open" Name = "Open" GroupCode = "Edit" GroupName = "Edit">
<Item Code = "Word" Name = "Word Document" GroupCode = "CommonDocument" GroupName = "regular"/>
<Item Code = "Excel" Name = "Excel document" GroupCode = "CommonDocument" GroupName = "regular"/>
<Item Code = "CustomDocument" Name = "custom document" GroupCode = "CustomDocument" GroupName = "Custom"/>
</Item>
<Item Code = "Save" Name = "Save" GroupCode = "Edit" GroupName = "Edit"/>
<Item Code = "Exit" Name = "Exit" GroupCode = "Exit" GroupName = "Exit"/>
</RightMenuItems>

Resolution method:

Copy codeThe Code is as follows:
$ (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;
};

The above is all the code for parsing XML using recursion in javascript. It is super simple and very useful. Please refer to it for your reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.