RK* - rikkertkoppes.com

thoughts

YUI DataSource xml parser extension

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).

  1. yui datasource responseschema XML xpath - Google Search (5)

comment

Tim (2009-10-03)

Thanks I'm new to YUI and this was exactly what I was after. Thanks, for the great code.

sicherste Casinos online (2010-04-02)

Jep.Net is a library for parsing and evaluating mathematical expressions. With this package you can allow your users to enter an arbitrary formula as a string, and instantly evaluate it. Jep.Net supports user defined variables, constants

Add comment
older articles

AdministrationAtom feed