md1
md1

Reputation: 15

Ext JS 4 : Reading data from an XML string

I am trying to load data from an XML string from client side. The XML reader is not getting the data from the xml string. Here is the code

  Ext.define('User', {    extend: 'Ext.data.Model',
    fields: ['firstname', 'lastname', 'phone']
});

var storeT =  Ext.create('Ext.data.Store', {    model: 'User',
    data:  '<users><user><firstname>Jack</firstname><lastname>Jobs</lastname><phone>1234567890</phone></user></users>', 
    autoLoad: true,
    proxy: {
        type: 'memory',
        reader: {
            type: 'xml',
            root: 'users',                            
            }    
    }
});

var user = storeT.first();
console.log("First Name " + user.get('firstname') );

The StoreT has no records. Any hints why it failed to get data?

Thanks in advance.

Upvotes: 0

Views: 3774

Answers (2)

md1
md1

Reputation: 15

Thanks bmoeskau and Evan, I could resolve the issue with your help. Here is the working code: I have added a function to get the XMLDoc..

function GetXMLDoc()     {  

         var xmlstring1 = "<users>" +
        "<user><firstname>Jack</firstname><lastname>Jobs</lastname><phone>1234567890</phone></user>" +
        "</users>"  ;  

        var doc;
        if(window.ActiveXObject){
          doc = new ActiveXObject("Microsoft.XMLDOM");
          doc.async = "false";
          doc.loadXML(xmlstring1);
        }else{
          doc = new DOMParser().parseFromString(xmlstring1,"text/xml");
        }

        console.log("xml", xmlstring1); 
        return doc;

  }

Changed the reader as follows:

    var storeT =  Ext.create('Ext.data.Store', {
        model: 'MyUser',
        data:  GetXMLDoc(), 
        autoLoad: true,
        proxy: {
            type: 'memory',
            reader: {
            type: 'xml',
            root: 'users',
            record: 'user',                     
            }
        }
    }); 


    var user = storeT.first();
    console.log("First Name " + user.get('firstname') );

Thanks..

Upvotes: 0

Brian Moeskau
Brian Moeskau

Reputation: 20429

The XmlReader's record config is required. Try adding this to your reader config:

    record: 'user'

Upvotes: 2

Related Questions