thoughts
YUI DataSource xml parser extension
2009-07-08
While utilizing the YUI DataSource utility I stumbled upon a problem. My XML document was formatted as follows:
<foo>
<item><!-- item data --></item>
<item><!-- item data --></item>
</foo>
<bar>
<item><!-- item data --></item>
<item><!-- item data --></item>
</bar>
The resultNode for the responseSchema is item, however, I only wanted items in foo, not in bar. I extended my dataSource instance as follows:
myDataSource = new YAHOO.util.DataSource("xmlSource");
myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML;
myDataSource.responseSchema = {
resultPath: "foo.item",
fields: ['a','b']
};
myDataSource.parseXMLData = function(oRequest, oFullResponse) {
var subdoc,path = this.responseSchema.resultPath.split(/[\.\/]/g);
if (path.length===1) return this.constructor.prototype.parseXMLData.apply(this,arguments);
try {
while (path.length>1) {
subdoc = oFullResponse.getElementsByTagName(path.shift())[0];
}
this.responseSchema.resultNode = path.shift();
return this.constructor.prototype.parseXMLData.call(this,oRequest,subdoc);
} catch(e) {
return {error: true};
}
}
Note I replaced resultNode with resultPath. This path can be dot or slash separated node names to resemble JavaScript dot notation as well as XPath notation. After crawling the path, the resultNode parameter is set and the original method is called.
Additional resources (top 15)
Below is a list of additional resources that might contain extra information about the subject at hand. These are all sites linking to this one (i.e. backtracking).
