PDA

View Full Version : Populate XMLStore from a generated XmlString?



jimmifett
27 Aug 2010, 12:34 PM
Is it possible to load data into an XML Store from an xml string or object generated on the fly?

MemoryProxy seems to be the direction I want to head I think, can anyone give me some pointers using it if it can do what i'd like it to do?

laurentParis
27 Aug 2010, 1:43 PM
Condor give an excellent answer for this question on this thread
http://www.sencha.com/forum/showthread.php?54631-Can-XMLreader-read-XML-String&p=260298

laurentParis
27 Aug 2010, 2:01 PM
Ext.data.XmlStore insert JSON object into store
reader of XmlStore (Ext.data.XmlReader), convert Xml document to Json Object

if you create your XML code on "fly", I suppose that you have a string variable like


var datas = '<?xml version="1.0" encoding="UTF-8"?><items><results>2</results><row><id>1</id><name>foo</name></row><row><id>2</id><name>foo2</name></row></items>';

but a string is not an Xml document
so, your first operation must be to convert string To Xml document

Answer gived by condor into another post (http://www.sencha.com/forum/showthread.php?54631-Can-XMLreader-read-XML-String&p=260298)



var xmlData = '<users total="1">' +
'<user><firstname>Jack<firstname><lastname>Jobs</lastname><phone>1234567890</phone></user>' +
'<user><firstname>Jenn<firstname><lastname>Syms</lastname><phone>4561230789</phone></user>' +
'</users>';

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

var xmlReader = new Ext.data.XmlReader({
record: 'user',
id: 'firstname',
totalRecords: '@total',
fields: ['firstname', 'lastname', 'phone']
});
var store = new Ext.data.Store({
reader: xmlReader,
data: doc
});

jimmifett
30 Aug 2010, 7:00 AM
ah! excellent! data:doc was exactly what I was looking for!