Results 1 to 3 of 3

Thread: XMLStore to select a specific node

  1. #1
    Sencha Premium Member
    Join Date
    Dec 2010
    Location
    San Diego, CA
    Posts
    49
    Answers
    1
    Vote Rating
    1
      0  

    Default XMLStore to select a specific node

    I have a XML file with the following schema

    Code:
    <root>
     <item>
      <subitem name="parent">
      <list>
       <subitem name="c1">
       <subitem name="c2">
       <subitem name="c3">
     </list>
    </item>
    </root>
    The issue for me here is the subitem is under two parent nodes. I would like to construct the model with subitem under the item as the parent and all the remaining subitems under list as the children of that. Some thing like below

    Code:
    Item
     name:"parent"
     children : [ itemc1,itemc2,itemc3]
    IS this possible using XMLStore. When i use item as the root node and subitem as the record node. The store is pulling all the subitems.

    Appreciate your help.

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,369
    Answers
    3997
    Vote Rating
    1514
      0  

    Default

    You could use associations:

    Code:
    Ext.define('MyModel', {
        extend : 'Ext.data.Model',
    
        fields : [
            { name : 'name', mapping : 'subitem@name' }
        ],
    
        hasMany : {
            model          : 'SubModel',
            name           : 'list',
            associationKey : 'list',
            reader         : {
                type   : 'xml',
                root   : 'list',
                record : 'subitem'
            }
        }
    });
    
    Ext.define('SubModel', {
        extend : 'Ext.data.Model',
    
        fields : [
            { name : 'name', mapping : '@name' }
        ]
    });
    
    new Ext.data.Store({
        autoLoad  : true,
        model     : 'MyModel',
        proxy     : {
            type   : 'ajax',
            url    : '/data/xml.xml',
            reader : {
                type   : 'xml',
                root   : 'root',
                record : 'item'
            }
        },
        listeners : {
            load : function(store, records) {
                var record = records[0],
                    list   = record.list();
    
                console.log(list);
            }
        }
    });
    Of course your sample XML is incorrect:

    Code:
    <root>
        <item>
            <subitem name="parent" />
            <list>
                <subitem name="c1" />
                <subitem name="c2" />
                <subitem name="c3" />
            </list>
        </item>
    </root>
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  3. #3
    Sencha Premium Member
    Join Date
    Dec 2010
    Location
    San Diego, CA
    Posts
    49
    Answers
    1
    Vote Rating
    1
      0  

    Default

    Hi,
    This case works exactly the way i expect.

    Code:
    <root>
        <item>
            <subitem name="parent" />
            <list>
                <subitem name="c1" />
                <subitem name="c2" />
                <subitem name="c3" />
            </list>
        </item>
    </root>
    I also have to parse a nested structure of the same xml like below,

    Code:
    <root>
        <item>
            <subitem name="parent" />
            <list>
                <subitem name="c1">
                    <list>
                        <subitem name="c1c1">
                         <list>
                                   <subitem name="c1c1c1"/>
                          </list>
                          </subitem>
                        <subitem name="c1c2"/>
                    </list>
                 </subitem>
                <subitem name="c2" />
                <subitem name="c3" />
            </list>
        </item>
    </root>
    I would like to store each subitem with its list as a nested structure. The XmlStore with the above configuration does not works because of the nesting. How should i use the store readers with this structure.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •