1

I am trying to parse an XML file with TBXML and put the values into a multidimensional NSMutableArray but the problem is that the values don't get added into the array, if I output the values then it returns null and 0.

This is my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self downloadAndParseXml:@"http://www.w3schools.com/XML/note.xml"];

    // RETURNS 0
    NSLog(@"weatherArray count: %d", [weatherArray count]);

    // RETURNS NULL
    NSLog(@"weatherArray value: %@", weatherArray[0][0]);
}

- (void)downloadAndParseXml:(NSString *)xmlUrl
{
    // Allocating the array
    weatherArray = [NSMutableArray array];

    TBXMLSuccessBlock sBlock = ^(TBXML *tbxmlDocument) {
        if (tbxmlDocument.rootXMLElement)
            [self fillArrayWithXmlContents:tbxmlDocument.rootXMLElement];
    };

    TBXMLFailureBlock fBlock = ^(TBXML *tbxmlDocument, NSError *error) {
        NSLog(@"Failureblock: %@\n%@", [error localizedDescription], [error userInfo]);
    };

    [TBXML newTBXMLWithURL:[NSURL URLWithString:xmlUrl] success:sBlock failure:fBlock];
}

- (void)fillArrayWithXmlContents:(TBXMLElement *)element {
    do {
        if (element->firstChild)
            [self fillArrayWithXmlContents:element->firstChild];

        if ([[TBXML elementName:element] isEqualToString:@"note"]) {

            // Adding values into the multidimensional array
            [weatherArray addObject:[NSArray arrayWithObjects:
                [TBXML textForElement:[TBXML childElementNamed:@"to" parentElement:element]],
                [TBXML textForElement:[TBXML childElementNamed:@"from" parentElement:element]],
                [TBXML textForElement:[TBXML childElementNamed:@"heading" parentElement:element]],
                [TBXML textForElement:[TBXML childElementNamed:@"body" parentElement:element]], NULL]];

            // Correct value is getting logged
            NSLog(@"weatherArray value: %@", weatherArray[0][0]);
        }
    } while ((element = element->nextSibling));
}
8
  • what does tbxmlDocument.rootXMLElement prints? Commented Aug 29, 2013 at 7:46
  • if I use NSLog the whole XML document is getting printed with the correct values Commented Aug 29, 2013 at 7:46
  • is ur class ARC enabled? Commented Aug 29, 2013 at 7:58
  • @iOSCoder Yes it is, by the way the values DO get logged when I NSLog it inside the do while loop Commented Aug 29, 2013 at 8:00
  • 1
    feels like some issue with the thread..as ur using blocks... Commented Aug 29, 2013 at 8:11

1 Answer 1

1

Probably what you need to do is wait for the process to get completed and the block literally doesn't know what your doing inside it or i would say the inside function is not been captured by the block.so use the array(call the function which is using your array) only after block gets completed.

Example:

TBXMLSuccessBlock s = ^(TBXML *tbxml) {
    NSLog(@"yes");
    // Do something with TBXML object "tbxml/add it ur array
   //Here you call the function which is about to use your array.

};

//Not outside the block.

EDIT : Refer this==> How do I avoid capturing self in blocks when implementing an API?

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

1 Comment

Thank you very much, This is the solution I need since I can do the stuff I want outside that method

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.