2

after a HTTP Post I retrieve an xml response like the following:

<result value="OK">
    <user id="1">
            <name>admin</name>
            <rank>0</rank>
            <picture_count>0</picture_count>
            <comment_count>0</comment_count>
            <has_profile>1</has_profile>
    </user>
</result>

I'd like to extract the user ID, but I don't know how to do this. I tried with GDataXML Parser, because this is already integrated in my project, but I don't know how to get a value inside a html tag.

I hope you can help me. If there is no solution with XMLParser, would you recommend regular expressions? In this case, I would appreciate a solution for the regex expression, I'm not very good at this :)

Thanks in advance.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
s4lfish
  • 633
  • 1
  • 11
  • 25
  • use `attributeForName` for user tag element. For more information see this [stackoverflow link](http://stackoverflow.com/questions/3028020/ask-for-an-example-code-for-parsing-xml-and-get-attributes-by-using-gdata-api) – Lorenzo B Jan 05 '12 at 13:37
  • Thanks, but if i use `GDataXMLElement *elem = doc.rootElement; NSString *userID = [[elem attributeForName:@"user id"]stringValue]; ` this returns null. – s4lfish Jan 05 '12 at 21:07
  • `attributeForName:@"id"` is correct. The attribute is `id` and not `user id`. Hope it helps. – Lorenzo B Jan 05 '12 at 21:52
  • `NSString *userID = [[elem attributeForName:@"id"]stringValue];`is still null. I also tried it with @"value" and this returns OK ... don't know why @"id" won't work. Do i have to work with `GDataXMLNode`? But i have no idea how. – s4lfish Jan 05 '12 at 22:49

1 Answers1

4

This is the code I use to retrieve the user attribute. It works.

NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];

NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData 
                                                           options:0 error:&error];

NSArray *userElements = [doc.rootElement elementsForName:@"user"];

for (GDataXMLElement *userEl in userElements) {

    // Attribute
    NSString *attribute = [(GDataXMLElement *) [userEl attributeForName:@"id"] stringValue];        
    NSLog(@"attribute for user is %@", attribute);

    // Name
    NSArray *names = [userEl elementsForName:@"name"];
    if (names.count > 0) {
        GDataXMLElement *name = (GDataXMLElement *) [names objectAtIndex:0];

        NSLog(@"name for user is %@", name.stringValue);
    }

    // Rank
    NSArray *ranks = [userEl elementsForName:@"rank"];
    if (ranks.count > 0) {
        GDataXMLElement *rank = (GDataXMLElement *) [ranks objectAtIndex:0];

        NSLog(@"rank for user is %@", rank.stringValue);
    }

    // Do the same for other tags...     
}

[doc release];
[xmlData release];

The file test.xml is the same that you retrieve from the xml response. So you need to change the second line of this snippet of code. Maybe you can call initWithData method of class NSData.

You can put this code where you want. Maybe you can create an other class that works as a centralized parser. Hope it helps

EDIT

Put these two lines before for instruction.

NSString *valAttribute = [(GDataXMLElement *) [doc.rootElement attributeForName:@"value"] stringValue];        
NSLog(@"value attribute for result is %@", valAttribute);

The explanation is quite simple. With doc.rootElement you retrieve the entire xml document, from <result> to </result>

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Thank you very much. I didn't build this user array with these single user elements. This code works like a charm. – s4lfish Jan 06 '12 at 09:24
  • Little question afterwards: I'm trying to get the result value, with building the array `NSArray *resultElements = [doc.rootElement elementsForName:@"result"];` and then the same like get ´attributeForName@"value"` but the array is null. – s4lfish Jan 08 '12 at 10:13
  • Ok, @flexaddicted, but how to extract `Array`. I have array inside element and I don't know how to extract it. – new2ios Jun 03 '15 at 12:48