0

I just want to parse a String received from a Post Response, I know there are lib/classes like NSXMLParse and apple provides some example but not what I want, or I'm not ready to understand that code.

I receive this:

<object>
<id>1</id>
<alias>juan</alias>
<email>jps@sol.pro</email>
</object>
<object>
<id>2</id>
<alias>juana</alias>
<email>jpsa@sol.pro</email>
</object>

Then I need to parse, and get the data like this:

NSString *xmlThing = [response];
for xmlThing in-all <object>
{
    uint id = <id>1</id>
    NSString *alias = <alias>juan</alias>
    NSString *email = <email>email@email.com</email>
}

Why like this? because I think is the easiest way to do and parse all kind of html, xml, etc... files.

I appreciate all kind of help.

Alejandro L.
  • 1,066
  • 5
  • 17
  • 38
  • 5
    `NSXMLParser` looks like it would be the right choice for your example. What part about it are you having a hard time with? – Mike D Feb 22 '13 at 14:56
  • Actually I don't understand the class reference `https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html` which method I should use to know how many are in the xml file? I need to know to loop all of them, then how I tell to return the value from a label? – Alejandro L. Feb 22 '13 at 15:05
  • 1
    `NSXMLParser` uses a pattern called delegation. See [this SO post](http://stackoverflow.com/a/1045854/620197) about delegation. It is used extensively through out all of the iOS/Foundation/UIKit APIs. The answer below is straight forward and should answer your question directly. – Mike D Feb 22 '13 at 15:14

3 Answers3

6

You can use XMLReader class to parse XML file with little effort. for more detail checkout https://appengineer.in/2013/08/03/xml-parsing-using-xml-reader-in-objective-c/

Mahendra Y
  • 1,941
  • 20
  • 26
4

You can use NSXMLParser and its delegate methods.

Example: In *.h file add NSXMLDelegate:

@interface YourClass: NSObject <NSXMLParserDelegate>

*.m file:

    @interface YourClass()
    @property NSMutableString *currentXMLValue;
    @property NSMutableArray *objects;
    @end

    @implementation YourClass{
    -(void) processXML {
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: data];
    self.objects = [[NSMutableArray alloc] init];
    [xmlParser setDelegate: self];
    [xmlParser parse];
    }

    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        //each time part of string is found append it to current string
        [self.currentXMLValue appendString:string];
    }

//here you can check when <object> appears in xml and create this object
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
    //each time new element is found reset string
    self.currentXMLValue = [[NSMutableString alloc] init];
    if( [elementName isEqualToString:@"object"])
    {
        self.obj= [[YourObject alloc] init];
    }
}
//this is triggered when there is closing tag </object>, </alias> and so on. Use it to set object's properties. If you get </object> - add object to array.
    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {

        if ([elementName isEqualToString:@"id"]) {
           obj.id = [self.currentXMLValue intValue];
        }
        else if ([elementName isEqualToString:@"alias"]) {
           obj.alias = self.currentXMLValue;
        }
        else if ([elementName isEqualToString:@"object"]) {
        if (self.objects) {
            [self.object addObject:obj];
        }
        }
    //and so on
    } 
    }
gladimdim
  • 752
  • 6
  • 9
0

well the above answer seems perfect,but i would advice you to use some other parser if you are having large amount of data and faster processing speed you can probably use TBXML,xml comparison here

sin
  • 726
  • 7
  • 16