XPath – An efficient way to parse xml



XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C). - courtesy: Wikipedia.org

XPath is a regular expressions style query language for selecting / extracting nodes from a xml document. Most of you have already seen the E4X standard introduced in as3 and how it speeds up your xml parsing. The new XML class in as3 negates the legacy XML class, which is also known as XMLDocument class now.

The reason to prefer this style over the “firstchild.childnode” style , is you can directly select nodes at any depth without having to parse down the entire family tree. This is one of the main reasons why As3 parsing seems more sensible than As2. Often you will b involved in projects where you need to parse a rss feed. You will notice the RSS feeds maintain a pretty complex structure, and a pretty good depth.  Places like these are , when you will really notice that the xml parsing time matters a lot.

Although As3 is pretty much out there and is undoubtedly the future you still need to deal with As2 for many reasons, such as compatibility / deeper language reach / Flash Lite and many times the specific business model may require you to code in As2. As of today flash lite is running with versions 2.0 /2.1 / 3.0 / 3.1  successfully across millions of phones. All these versions run Action script 2.0 primer as the supported language. So that’s another reason why not to overlook As2.

you can download Xpath for As2 from http://osflash.org/xpath4as2

Example : RSS feed parsing with XPath:

Consider we load a rss feed from feedburner. http://feeds.feedburner.com/MobileInc?format=xml

This is a typical rss feed. And if you open it in the browser (I prefer FireFox) you will see to reach down to <item> nodes you really need to do quiet a lot of parsing.

For example to reach down to the title of the first item in the feed, i need to go a long way down:

var _loc2 = my_xml.firstChild.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.childNodes;
var firsttitle = my_xml.firstChild.firstChild.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.childNodes;

(of course you can do better :) )

Now the XPath way to do this would be:

var firsttitle = XPath.selectNodesAsString(my_xml, "//item[position()=1]/title/text()");

So if you see This really saves your time and lines of codes that go into your program. However I mostly recommend doing xml parsings on server side for flash lite. this way you can discard unnecessary data and suck up only what your application  needs, instead of loading the entire xml.

For more examples on XPath As2 erefer to : http://osflash.org/xpath4as2

and for general info on XPath refer to: http://www.w3schools.com/XPath/default.asp

Popularity: 7% [?]




November 8, 2009  Tags: , ,   Posted in: Actionscript 2.0, Flash Lite

Leave a Reply