PDA

View Full Version : XmlReader reading blank attribute



cwolves
7 Jun 2007, 9:16 AM
sample xml:

<xml><item foo="foo" bar="" /></xml>

sample Record:

var rec = Ext.data.Record.create([
{name:'foo', type:'string', mapping:'@foo'},
{name:'bar', type:'string', mapping:'@bar'}
]);"


When reading, bar is read as "undefined", not an empty string. Without adding a renderer grids will show the text "undefined".

Further, the data is actually stored as the string "undefined" rather than undefined. This effects reading the data in any way, be it through item.data or a renderer.

tryanDLS
7 Jun 2007, 10:42 AM
There does seem to be an issue here. I'm not an xpath guy, so I don't know if there is any significance to using '@foo' and '@bar' to retrieve attributes or if it can only find one that way. Looking at DomQuery.selectValue, it's only going after the node.firstChild. It doesn't find the value and gets back undefined. This further complicates the problem b/c then it doesn't even set the defaultValue if you pass it on the field def. This may also be an issue with the way the browser parses the xml - it's setting the nodevalue as undefined instead of blank.

jack.slocum
8 Jun 2007, 8:54 PM
It turns undefined into a string because the convert type:"string" tells it to. Otherwise the grid would blank it automatically.

I have made a change in SVN to make the string convertor more forgiving of null/undefined values by returning ''. That should fix the issue.

tryanDLS
9 Jun 2007, 8:43 AM
I think there's a deeper issue of how DomQuery handles missing values in attributes vs nodes. Given the following code, which can be dropped into examples, default values are returned for nodes, but not attributes. There is also a difference in behavior between FF and IE6. Items 6-10 do return default values in IE6.


<html>
<head>
<script type="text/javascript" src="../adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
var rec = Ext.data.Record.create([
{name: 'foo', type: 'string', mapping:'@foo', defaultValue:'no-Foo'},
{name: 'bar', type: 'string', mapping:'@bar', defaultValue:'no-Bar'}
]);

var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'xml-test.xml'}),
reader: new Ext.data.XmlReader({
record: 'item'
}, rec)
});

ds.on('load', function(store, recs) {
var s ='';
var cnt = recs.length;
for (var i=0;i<cnt;i++) {
s += i + ') Foo:' + recs[i].data.foo + '; Bar:' + recs[i].data.bar + '\n';
}
alert(s);

var doc = store.reader.xmlData;
var root = doc.documentElement || doc;

var q = Ext.DomQuery;
s = '';
for (var i=0;i<11;i++) {
s += i + ') Node:' + q.selectValue('item' + i,root, 'default'+i) + '; Foo:' + q.selectValue('item' +i +'@foo',root, 'F'+i) + '; Bar:' + q.selectValue('item' +i +'@bar',root, 'B'+i) + '\n';
}
alert(s);
});
ds.load();
});
</script>
</head>
<body>
</div>
</body>
</html>


xml-test.xml


<xml>
<item bar="A" foo="B" />
<item bar="" foo="B" />
<item bar="A" foo="" />

<item0 bar="A0" foo="B0" />
<item1 bar="" foo="B1" />
<item2 bar="A2" foo="" />
<item3 bar="A3" foo="B3">3</item3>
<item4 bar="" foo="B4">4</item4>
<item5 bar="A5" foo="">5</item5>
<item6 bar="A6">6</item6>
<item7 foo="B7">7</item7>
<item8>8</item8>
<item9></item9>
<item10 />
</xml>

CableDawg
31 Aug 2007, 4:58 AM
Has there been any update on this issue?

I am currently running in to the same problem. I have an attribute I would like to read in that is sometimes empty. In that case, I would like to apply the defaultValue.

Other than modifying readRecords, I can't see any way that a defaultValue would ever get applied to an empty attribute (of string type).

Any suggestions? (Or has this been fixed?)

Also, I noticed that selectValue on the blank attribute will return UNDEFINED in FF/IE and an empty string in Safari 2.