Parse xml recursively in js, parse xml recursively in js
Xml structure:
<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:
$ (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;
};