Getting xml data from a web service (Part 2 – RaprureXML 101)

In the first part, Getting xml data from a web service (Part 1 – AFNetworking 101), we managed to get the raw xml data from a web service using the AFNetworking library.

RaptureXML is a simple, block-based XML library for the iOS platform that provides an expressive API that makes XML processing easy!

The procedure to install it according the official documentation from GitHub ZeBlanc' s page is as follow:

To install manually, there's just a few simple steps:

  • Copy the RaptureXML/RaptureXML folder into your own project and import "RXMLElement.h" somewhere (e.g., your PCH file).
  • Link in libz.dylib to your target.
  • Link in libxml2.dylib to your target.
  • In your build settings, for the key "Header Search Paths", add "$(SDK_DIR)"/usr/include/libxml2

Mr Jan Sanchez has released and AFNetworking extension to make it easy to use RaptureXML with it.

You can find it in his GitHub Page AFRaptureXMLRequestOperation,download it and add files to your code project.

We will use RaptureXML in order to parse xml data to iOS easy manageable objects.

Let's start the editing of our code (don't forget to add AFRaptureXMLRequestOperation files to your project):

//  AFmyAPIClient.h

#import "AFmyAPIClient.h"
#import "AFRaptureXMLRequestOperation.h"

static NSString * const kAFApiBaseURLString = @"http://services.testwebservice.gr/";

@implementation AFmyAPIClient

+ (AFmyAPIClient *)sharedClient
{
static AFmyAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
_sharedClient = [[AFmyAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kAFApiBaseURLString]];
});

return _sharedClient;
}

- (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

return self;
}
@end

and in your ViewController.m code

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    AFepricesClient *myClient = [AFepricesClient sharedClient];

    [myClient getPath:@"getCategories?ui=123456" //it's an example path
           parameters:nil
              success:^(AFHTTPRequestOperation *operation, id responseObject) {

                  NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

                  RXMLElement *rootXML = [RXMLElement elementFromXMLString:responseString encoding:NSUTF8StringEncoding];
                  NSArray *categories = [rootXML children:@"category"];

                  [rootXML iterateElements:categories usingBlock:^(RXMLElement *categoryElement) {
                      NSLog(@"ID: %d, Name: %@", [categoryElement child:@"categoryid"].textAsInt, [categoryElement child:@"name"].text);
                  }];
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"There was an error logging into Reader - %@", [error localizedDescription]);
              }];

}

If you build and run your project you will get an output like this,

2013-04-25 21:12:19.857 iPhoneShopping[1061:c07] ID: 5, Name: ΡΥΖΙ ΜΑΚΡΟΣΠΕΡΜΟ (ΤΥΠΟΥ ΚΑΡΟΛΙΝΑ)
2013-04-25 21:12:19.857 iPhoneShopping[1061:c07] ID: 6, Name: ΡΥΖΙ ΜΑΚΡΟΣΠΕΡΜΟ (ΤΥΠΟΥ ΜΠΑΡΜΠΑ - ΜΠΕΝ)
2013-04-25 21:12:19.857 iPhoneShopping[1061:c07] ID: 7, Name: ΡΥΖΙ ΣΤΡΟΓΓΥΛΟΣΠΕΡΜΟ (ΤΥΠΟΥ ΓΛΑΣΕ)
...

This is great!By using the RaptureXML we got a NSArray with the categories and furthermore we can iterate through the array and access the categoryid and the name of each category. So simple!

Want to learn more tips and tricks and RaptureXML like a pro?

Read Jonh's Blanco tutorial.

2 thoughts on “Getting xml data from a web service (Part 2 – RaprureXML 101)

Leave a Reply

Your email address will not be published. Required fields are marked *