PDA

View Full Version : XML reader reading data from a XML string



Mohandas
5 Mar 2013, 5:35 PM
Trying to extract data from a xml string from client side.

The data model is

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

Store and proxy are used as

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',
}
}
});


usage:

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

The data is not getting read from the xml string. Any hints?
I have already tried prefixing the xml string with "<?xml version="1.0" encoding="utf-8"?>".


Thanks.

droessner
6 Mar 2013, 6:16 AM
You'll have to parse the xml string and then pass the xml document as the data. Something like:



var xmlString,
xmlDocument;

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

Mohandas
7 Mar 2013, 10:25 AM
Thanks droessner. It reolved the issue. I have also added record: 'user' in the reader
Thanks