Posts Tagged ‘XML’

Tree XML parser for iPhone

When parsing XML files, there are two ways. One is to use an event parser and the other one is to use a tree parser. Cocoa provides classes for the two different type of parsing. NSXMLDocument provides a tree parser functionality while NSXMLParser provides an event parser functionality.

However, for devices like iPhone where memory usage is critical, using tree parser is not recommended. I guess that is why Apple doesn’t provide NSXMLDocument on iPhone/iPod touch.

However, the model of event parsing may not be acceptable by your code. You may need to use XPath/XQuery.
Then the underlying open source library, libxml2 can provide the functionality, but you want Cocoa style approach.

At Cocoa with Love, very nice explanation and code sample to write wrapper for the libxml2 was posted. It has a title, “Using libxml2 for XML parsing and XPath queries in Cocoa”

Also, there are two tree XML parsers for iPhone.

Also at StackOverFlow, there is a very nice explanation why using tree parsers on mobile device is not good. Although your iPhone/iPod touch may have plenty of memory space, due to its origin ad the fact that it is mobile device, I think the explanation still holds.

Powerful XPath and XQuery : How to extract RSS feeds from an HTML file

Wow.. XQuery coupled with XPath is really powerful.

With this HTML, how can hrefs of link where its rel attribute is “alternate” be retrieved?

<html xmlns="http://www.w3.org/1999/xhtml">

<head profile="http://gmpg.org/xfn/11">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

	<title>iPhone Programming Tutorial</title>
	
	<!--&#91;if IE&#93;>
	<style> 
	#content { top:0px;	}
	</style>
	<!&#91;endif&#93;-->
	
	<!--&#91;if IE 6&#93;>
	<style type="text/css">
	.left-content {
		float: left;
		width: 620px;	
	}
	.text-box {
		float: left;
		width: 620px;
		padding: 0 26px 0 27px;
	}
	</style>
	<!&#91;endif&#93;-->
	
	

<meta name="generator" content="WordPress 2.8.1" /> <!-- leave this for stats -->

<link rel="stylesheet" href="http://icodeblog.com/wp-content/themes/bluez/style.css" type="text/css" media="screen" />
	
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://icodeblog.com/feed/" />

<link rel="alternate" type="text/xml" title="RSS .92" href="http://icodeblog.com/feed/rss/" />

<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="http://icodeblog.com/feed/atom/" />

<link rel="pingback" href="http://icodeblog.com/xmlrpc.php" />

	<link rel='archives' title='August 2009' href='http://icodeblog.com/2009/08/' />
	<link rel='archives' title='July 2009' href='http://icodeblog.com/2009/07/' />
	<link rel='archives' title='June 2009' href='http://icodeblog.com/2009/06/' />
	<link rel='archives' title='May 2009' href='http://icodeblog.com/2009/05/' />
	<link rel='archives' title='March 2009' href='http://icodeblog.com/2009/03/' />
	<link rel='archives' title='February 2009' href='http://icodeblog.com/2009/02/' />
	<link rel='archives' title='January 2009' href='http://icodeblog.com/2009/01/' />
	<link rel='archives' title='December 2008' href='http://icodeblog.com/2008/12/' />
	<link rel='archives' title='November 2008' href='http://icodeblog.com/2008/11/' />
	<link rel='archives' title='October 2008' href='http://icodeblog.com/2008/10/' />
	<link rel='archives' title='September 2008' href='http://icodeblog.com/2008/09/' />
	<link rel='archives' title='August 2008' href='http://icodeblog.com/2008/08/' />
	<link rel='archives' title='July 2008' href='http://icodeblog.com/2008/07/' />

Proper XQuery is

for $p in /html/head/link
where $p/@rel = "alternate"
return $p/@href

It will return :

href=”http://icodeblog.com/feed/&#8221;
href=”http://icodeblog.com/feed/rss/&#8221;
href=”http://icodeblog.com/feed/atom/&#8221;

Very powerful and very impressive!

Tutorials for XQuery and Cocoa

    I needed to extract information from a Final Cut Pro project file in XML format. There are two ways to do so. One is to use a event-driven method using NSXMLParser, while the other approach is to use a Tree parser like NSXML.

    Apple provides a nice sample code for XML processing, which is located at /Developer/Examples/Foundation/XMLBrowser.
With this sample, you can apply many settings like “Pretty-Print” and so on. You can even try what result XQuery statement returns. So, this XMLBrowser sample code is quite a good test bed for your Objective-C/Cocoa coding.

    Along this, I found a good tutorial for the XQuery. ( You can understand the XQuery to XML as SQL to the relational database. ) There is another one here at the IBM developer works.

    Also, Apple provides a very interesting tips for the Final Cut.

Enjoy the XML coding!!!

%d bloggers like this: