Question

I received the following XML IQ and want to parse it in iOS. Please help!

     <iq type="result">
        <stories xmlns="success">

            <storyid>1</storyid>
            <title>THE TITLE</title>
            <picture>BASE64EncodedStringOfImage</picture>

            <storyid>2</storyid>
            <title>Love At First Site</title>
            <picture>BASE64EncodedStringOfImage</picture>

        </stories>
     </iq>

I tried the below code but it isn't working. It gets the values in successStoryArray but nothing afterwards. Please help!

    NSXMLElement *stories = [iq elementForName:@"stories"];
    NSArray *successStoryArray = [stories elementsForName:@"storyid"];
    NSLog(@"successStoryArray: %@", successStoryArray);
    for (NSXMLElement *i in successStoryArray)
    {
        NSXMLElement *title = [i elementForName:@"title"];
        NSString *sTitle = [title stringValue];
        NSLog(@"sTitle: %@", sTitle);
        NSXMLElement *picture = [i elementForName:@"picture"];
        NSString *sPicture = [picture stringValue];
        NSLog(@"sPicture: %@", sPicture);

    }
Was it helpful?

Solution

You are iterating over the wrong elements. NSArray *successStoryArray = [stories elementsForName:@"storyid"]; will get two elements: storyid with value=1 and storyid with value=2. Then you iterate over those elements and trying to extract title and picture, but title and picture are not child elements of . Title and picture are at the same level as storyid, they are children of .

There are two ways to fix it:

Extract 3 arrays, each containing the values for storyid, title and picture

NSArray *successStoryArray = [stories elementsForName:@"storyid"];
NSArray *titleArray = [stories elementsForName:@"title"];
NSArray *pictureArray = [stories elementsForName:@"picture"];

Then you can iterate them in order and items will match

Even better in my opinion, change the structure of the XML by wrapping each story in a element, like this:

     <iq type="result">
        <stories xmlns="success">
          <story>
            <storyid>1</storyid>
            <title>THE TITLE</title>
            <picture>BASE64EncodedStringOfImage</picture>
          </story>
          <story>
            <storyid>2</storyid>
            <title>Love At First Site</title>
            <picture>BASE64EncodedStringOfImage</picture>
          </story>
        </stories>
     </iq>

Then you can do:

    NSXMLElement *stories = [iq elementForName:@"stories"];
    NSArray *successStoryArray = [stories elementsForName:@"story"];
    NSLog(@"successStoryArray: %@", successStoryArray);
    for (NSXMLElement *i in successStoryArray)
    {
        NSXMLElement *storyId = [i elementForName:@"storyId"];
        NSString *sId = [storyId stringValue];
        NSLog(@"sId: %@", sId);
        NSXMLElement *title = [i elementForName:@"title"];
        NSString *sTitle = [title stringValue];
        NSLog(@"sTitle: %@", sTitle);
        NSXMLElement *picture = [i elementForName:@"picture"];
        NSString *sPicture = [picture stringValue];
        NSLog(@"sPicture: %@", sPicture);

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top