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).
- пример YAHOO.util.DataSource.TYPE_XML - Поиск в Google (3)
- xml parser in yui 2 - Google Search (3)
- yui DataSource xml attribute - Поиск в Google (2)
- xml parsing in yui - Google Search (2)
- yahoo datasource xml xml fields - Google zoeken (2)
- xml parser in yui 2 - Google Search (2)
- yui oFullResponse XML String - Google Search (2)
- YAHOO.util.DataSource.TYPE_XML - Google-Suche (2)
- yui datasource responseSchema - Google-Suche (2)
- yui responseSchema - Pesquisa do Google (2)
- resultnode yui path - Google Search (1)
- DataSource.responseSchema xpath - Google-søk (1)
- yui datasource ie xpath - Google Search (1)
- YUI DataSource path - Google Search (1)
- yui datasource prototype - Google Search (1)
