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>[email protected]</email>
</object>
<object>
<id>2</id>
<alias>juana</alias>
<email>[email protected]</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 protected]</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.

3
  • 5
    NSXMLParser looks like it would be the right choice for your example. What part about it are you having a hard time with? Commented Feb 22, 2013 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 <object></object> are in the xml file? I need to know to loop all of them, then how I tell to return the value from a <id></id> label? Commented Feb 22, 2013 at 15:05
  • 1
    NSXMLParser uses a pattern called delegation. See this SO post 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. Commented Feb 22, 2013 at 15:14

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Thank you, I forgot about it!
It should be an object which contains your id, alias, email.
@Monolo, thank you. I've updated my answer with your recommendation.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.