Raju
Raju

Reputation: 3469

How do I parse an NSString containing XML in Objective-C?

In my iPhone application, I have the following NSString:

NSString *myxml=@"<students>
    <student><name>Raju</name><age>25</age><address>abcd</address> 
    </student></students>";

How would I parse the XML content of this string?

Upvotes: 8

Views: 29219

Answers (5)

Cœur
Cœur

Reputation: 161

Download: https://github.com/bcaccinolo/XML-to-NSDictionary

Then you simply do :

NSDictionary *dic = [XMLReader dictionaryForXMLString:myxml error:nil];

Result is a NSDictionary *dic with dictionaries, arrays and strings inside, depending of the XML:

{
    students =     {
        student =         {
            address = abcd;
            age = 25;
            name = Raju;
        };
    };
}

Upvotes: 13

kaya kahveci
kaya kahveci

Reputation: 9

I think the best equivalent to XMLDocument is AbacigilXQ Library. You should look at it. I'm using it.

http://code.google.com/p/abacigilxq-library/

Upvotes: 0

A Salcedo
A Salcedo

Reputation: 6468

Like @Jon Hess mentioned, just create a wrapping class for the "optional" methods of the NSXMLParserDelegate. These methods help you separate the tasks that you might find useful when you parse your xml.

One really good online journal file I found is Elegant XML parsing with Objective-C. Phil Nash really took his time to show the basics of the parsing options at your reach. It can take a new programmer and guide him/her through the whole setup.

Loading the xml can be a modification of @Jon Hess method.

You can setup the:

-(void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
   attributes:(NSDictionary *)attributeDict{
}

to handle events on certain elements.

Also implement the:

-(void)parser:(NSXMLParser *)parser 
foundCharacters:(NSString *)string {
}

to place the strings found into a collection of objects.

Upvotes: 0

Another answer is: Don't use XML. Use a plist instead, which is written using XML but more easily parsable in Objective-C into distinct data types (NSArray for example has a method to convert a file or NSData plist into an NSArray).

Upvotes: 2

Jon Hess
Jon Hess

Reputation: 14247

You should use the NSXMLParser class

Here's a link to the documentation for that class: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html

Your code should look something like this:

@implementation MyClass
- (void)startParsing {
    NSData *xmlData = (Get XML as NSData)
    NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started %@", elementName);
}

Upvotes: 10

Related Questions