P0ed / XMLParser

XML Parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

XMLParser

Simple parser that builds a tree and provides easy access to the nodes, allowing to transform them into objects implementing a single method.

Example:

<rss>
	<channel>
		<title>
			W3Schools Home Page
		</title>
		<link>http://www.w3schools.com</link>
		<description>Free web building tutorials</description>
		
		<item>
			<title>RSS Tutorial</title>
			<link>http://www.w3schools.com/rss</link>
			<description>New RSS tutorial on W3Schools</description>
		</item>
		<item>
			<title>XML Tutorial</title>
			<link>http://www.w3schools.com/xml</link>
			<description>New XML tutorial on W3Schools</description>
		</item>
	</channel>
</rss>
XMLNode *rootNode = [XMLParser.parser parseData:fileContents];
XMLNode *channelNode = rootNode[@"channel"];
NSString *channelTitle = channelNode[@"title.#stringValue"];
NSURL *channelURL = channelNode[@"link.#URLValue"];
NSArray *items = channelNode[@"item[].#itemValue"];

To transform into Item you need to implement a method in XMLNode category

@implementation XMLNode (ItemValue)
- (Item *)itemValue {
	
	Item *item = [Item new];
	item.title = self[@"title.#stringValue"];
	item.link = self[@"link.#URLValue"];
	item.text = self[@"description.#stringValue"];
	
	return item;
}
@end

About

XML Parser

License:Apache License 2.0


Languages

Language:Objective-C 100.0%