I have been working on an application that uses NSXML classes to parse HTML sites and massage them for use as an eBook. I created a couple of handy categories for NSXMLElement and NSXMLNode to make my life easier.
NSXMLNode
I found that when doing XPATH searches through the source. I usually was only looking for one node (or the first node in the search). So i created a simple helper function:
-(NSXMLNode*):singleNodeForXPath:(NSString*)xPath error:(NSError**)error
This is simple wrapper around the nodesForXPath call that returns the first node found in the search path. If no items are found then it returns nil. If multiple items are found it returns only the first one.
The other call is:
-(NSString*) stringValueForNodePathAtXPath:(NSString*)path error:(NSError**)error
This simply returns the string value for the first node found in an xPath search.
These just provide a simple way to do a quick search and return the first items found.
NSXMLElement
Since i am reconstructing elements and possibly manipulating the tree, I needed a way to do a shallow copy of a node so that it has no children. So i have added:
-(NSXMLElement*) shallowCopy
This will copy the element and all its attributes as a standalone node, no children and no parents.
You can download the sourceĀ here.