Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: retrieving array using Xmlreader

  1. #1

    Question retrieving array using Xmlreader

    Hi,
    I am retrieving a record from database having following record format.

    ..........
    <items>
    <item>1</item>
    <item>2</item>
    </items>
    ...........

    when i try to retrieve this array using record.data.items it says it undefined.
    How can we retrieve the array information using xmlreader?

    Please let me know ASAP.

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    Code:
    reader: new Ext.data.XmlReader({
      record: 'item',
      idProperty: '/',
      fields: [{name: 'field1', mapping: '/'}]
    })

  3. #3

    Default

    thanks for you reply
    but the xml piece mentioned above is not the complete record xml.
    the array is the part of a record xml

    like
    <record>
    .........
    <items>
    <item>1</item>
    <item>2</item>
    </items>
    ............
    </record>

    now i am able to retrieve all other fields from the records using record.data.<fieldname>
    but not the arrray using record.data.items

  4. #4

    Default

    I think, what i need over here is how to define an array field while creating xmlreader object.

    Normal way is
    new Ext.data.XmlReader({
    id: 'xyz',
    record:
    'record'
    },
    [
    {name:
    'id'},
    {name: 'name'},
    {name:
    'uri'},
    {name:
    'description'}
    ]
    });
    what if i have an array element in the record.
    how should i define the field?



  5. #5
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    You would need to use a convert function, e.g.
    Code:
    {name: 'items', mapping: 'items', convert: function(v){
        var nodes = Ext.DomQuery.select('item', v), items = [];
        for(var i = 0, len = nodes.length; i < len; i++){
            var node = nodes[i];
            items.push(node.firstChild ? node.firstChild.nodeValue : null);
        }
        return items;
    }}

  6. #6

    Default

    Thanks for the code.
    But it seems its not workig.......
    i am getting v as null
    and nodes.length = 0

    let me know where is it going wrong?

  7. #7

    Default

    The exact structure is as given below.<items><item> <itemId>100</itemId> <itemName>1</itemName></item><item> <itemId>101</itemId> <itemName>1</itemName></item></items>I want to retrieve all the "item"s in an array for processing.appreciate your help...

  8. #8
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    You're confusing me...

    It this part of a larger document and do you want to read these items in a field or is this the entire document and do you want to read this in a store?

  9. #9

    Default

    Sorry.... let me make it clear.
    the complete xml is as follows.

    <itemDetails>
    <vendorId>123</vendorId>
    <locationId></locationId>
    <items>
    <item>
    <itemId>100</itemId>
    <itemName>1</itemName>
    </item>
    <item>
    <itemId>101</itemId>
    <itemName>1</itemName>
    </item>
    </items>
    </itemDetails>

    No i want to read it in a store using xmlreader.
    in normal scenario with basic field definition we can access any field as record.data.vendorId,record.data.locationId......
    similiraly, i want to access the array of items so i can access them as record.data.items[0].id,record.data.items[0].name
    Is it possible anyway?

    I hope i am clear now.........
    let me know if u need any other info.

    Thanks a bunch!!

  10. #10
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    This would create a field 'items' that holds an array of {itemId, itemName} objects:
    Code:
    {name: 'items', mapping: 'items', convert: function(v){
        var nodes = Ext.DomQuery.select('item', v), items = [];
        for(var i = 0, len = nodes.length; i < len; i++){
            var node = nodes[i];
            items.push({
                itemId: Ext.DomQuery.selectValue('itemId', node),
                itemName: Ext.DomQuery.selectValue('itemName', node)
            });
        }
        return items;
    }}

Page 1 of 2 12 LastLast

Posting Permissions

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