PDA

View Full Version : barebones XML data reader expecting JSON



DaveC426913
14 Sep 2010, 12:43 PM
I'm working on populating my lists with dynamic data.

Getting this error:
Uncaught SyntaxError: Unexpected token ILLEGAL

At this line in ext-touch.js
decode : function(s) {
return JSON.parse(s);
}


It seems to be expecting JSON by default. I'm sure there's a way to tell it to expect XML.


Ext.Ajax.request({
url: 'http://localhost:8080/Sencha/somedata.json',
success: function(response, opts) {
var obj = Ext.decode(response.responseText);
console.dir(obj);
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response.status);
}
});




<?xml version="1.0" encoding="UTF-8" ?>
<data>
<success>true</success>
<row id="1">poultry</row>
<row id="2">beef</row>
<row id="3">fish</row>
</data>

evant
14 Sep 2010, 4:01 PM
As it says in the docs, decode is a shortcut for Ext.util.JSON.decode. You don't need to do anything with it, the response object automatically returns XML, if available:



console.log(response.responseXML);

DaveC426913
15 Sep 2010, 6:06 AM
As it says in the docs, decode is a shortcut for Ext.util.JSON.decode. You don't need to do anything with it, the response object automatically returns XML, if available:



console.log(response.responseXML);


I see, so it doesn't have to be decoded, I am handed the XML itself. But I still have to parse it and convert it to usable objects, do I not? Then I've got to build a whole strucutre to get those objects into my list.

I must be misunderstanding something. If my assumptions are correct, using XML data is a huge part of what Ext/Sencha is meant to do, yet I do not find any demo examples use this responseXML technique. I must be on the wrong track.

evant
15 Sep 2010, 6:16 AM
That's just what happens with a normal Ajax request, nothing to do with Touch at all, it's ~always~ been this way.

DaveC426913
15 Sep 2010, 6:18 AM
That's just what happens with a normal Ajax request, nothing to do with Touch at all, it's ~always~ been this way.
Yes, I understand that so far I'm only dealing with an Ajax request.

But Ext does use Ajax call to get data into objects. I don't need to reinvent the wheel. I just want to use the standard Ext method for populating an object from XML.

I guess that's this "store" class.

fx-mike
15 Sep 2010, 7:55 AM
Take a look at the Ext.DomQuery, it should let you simplify your code significantly.

DaveC426913
15 Sep 2010, 7:59 AM
Take a look at the Ext.DomQuery, it should let you simplify your code significantly.

Ext.DomQuery seems to be all about CSS selectors. I don't understand how that would help me get my XML data loaded into my list.

fx-mike
15 Sep 2010, 8:15 AM
For example


Ext.DomQuery.select('row', response.responseXML);

would return an array containing all the row elements in your XML.

There is also a Ext.DomQuery.selectValue function which I can't get working right now (the docs seem to be a bit off here)

DaveC426913
15 Sep 2010, 8:34 AM
For example


Ext.DomQuery.select('row', response.responseXML);

would return an array containing all the row elements in your XML.


Oh! Thanks, I'll give that a try.

Actually, this works great. It automatically gives me the loop in which to process each item:


Ext.each(xmlObj.getElementsByTagName("row"),function(item,index){
}