Jan Sanchez

RaptureXML and AFNetworking

If you are like me, who loves to use RaptureXML for parsing XML documents, and to use AFNetworking when playing with REST APIs, then this post is for you.

I created an AFNetworking extension to make it easy to use RaptureXML.

Here is a sample on how to use it:

1
2
3
4
5
6
7
AFRaptureXMLRequestOperation *operation = [AFRaptureXMLRequestOperation XMLParserRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://legalindexes.indoff.com/sitemap.xml"]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, RXMLElement *XMLElement) {
   // Do something with XMLElement 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, RXMLElement *XMLElement) {
    // Handle the error
}];

[operation start];

If subclassing AFHTTPClient then we just need to use registerHTTPOperationClass to register it with AFRaptureXMLRequestOperation

1
2
3
4
5
6
7
8
9
10
11
12
13
- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFRaptureXMLRequestOperation class]];

    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
    [self setDefaultHeader:@"Accept" value:@"text/xml;level=1,application/xml;level=2"];

    return self;
}

I created this extension several months ago, but recently updated it to use ARC and included some minor tweaks based on some official extensions. Feel free to play around with it, fork it and send any pull requests.