Question

Hi I've looked everywhere for the last two days & can not find anything that works for me. I've managed to pull in the data from a url, and in my logs I get every single element and value displayed.

The step I can't figure out is how to add it to my "events" array, without having to manually search for each string since it automatically is doing it for me (as seen in my logs).

Here is my loadUrl method:

- (void)loadURL {

// Create a success block to be called when the asyn request completes
TBXMLSuccessBlock successBlock = ^(TBXML *tbxmlDocument) {
    NSLog(@"PROCESSING ASYNC CALLBACK");

    // If TBXML found a root node, process element and iterate all children
    if (tbxmlDocument.rootXMLElement)
        [self traverseElement:tbxmlDocument.rootXMLElement];

};

// Create a failure block that gets called if something goes wrong
TBXMLFailureBlock failureBlock = ^(TBXML *tbxmlDocument, NSError * error) {
    NSLog(@"Error! %@ %@", [error localizedDescription], [error userInfo]);
};

// Initialize TBXML with the URL of an XML doc. TBXML asynchronously loads and parses the file.
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://somexml.com/xml"] 
                           success:successBlock 
                           failure:failureBlock]; 

    events = [NSMutableArray array]; }

And here is my traverseElement method:

- (void) traverseElement:(TBXMLElement *)element {

do {
    // Display the name of the element
    NSLog(@"%@",[TBXML elementName:element]);


    // iterate all child elements of tbxml.rootXMLElement that are named "author"
    [TBXML iterateAttributesOfElement:element withBlock:^(TBXMLAttribute *attribute, NSString *name, NSString *value) {

        // Display name and value of attribute to the log window
        NSLog(@"%@->%@ = %@",[TBXML elementName:element], name, value);

    }];

    // if the element has child elements, process them
    if (element->firstChild) [self traverseElement:element->firstChild];

    // Obtain next sibling element
} while ((element = element->nextSibling));  


}

Here is the xml I'm pulling in:

<resultsPage totalEntries="6" perPage="50" page="1" status="ok">
<results>
<event uri="http://somexml.com/xml" popularity="0.863682" displayName="Radio 1's Hackney 
Weekend 2012" id="9234656" type="Festival" status="ok">
<location city="London, UK" lng="-0.128" lat="51.5078"/>
<series displayName="Radio 1's Hackney Weekend"/>
<end time="" date="2012-06-24" datetime=""/>
<start time="" date="2012-06-23" datetime=""/>
<performance displayName="Lucy Labeaux" billingIndex="5" id="23336188" billing="headline">
<artist uri="http://www.somexml.com/artistxml" displayName="Lucy Labeaux" id="1168415">
<identifier href="http://somexml.com.xml" mbid="4593d49a-7f67-46ba-9ec0-126bd676286f"/>
</artist>
</performance>

My log looks like this:

PROCESSING ASYNC CALLBACK
 resultsPage
 resultsPage->totalEntries = 6
 resultsPage->perPage = 50
 resultsPage->page = 1
 resultsPage->status = ok
 results
 event
 event->uri = http://example.com
 event->popularity = 0.863682
 event->displayName = Cooled Up Week 2012
 event->id = 9234656
 event->type = Festival
 event->status = ok
 location
 location->city = London, UK
 location->lng = -0.128
 location->lat = 51.5078
 series
 series->displayName = Cooled Up Week 2012
 end
 end->time = 
 end->date = 2012-06-24
 end->datetime = 
 start
 start->time = 
 start->date = 2012-06-23
 start->datetime = 
 performance
 performance->displayName = Lucy Labeaux
 performance->billingIndex = 1

So now that I've gone through all the elements,attributes etc, how do I link it to my table view, and row count? I feel like giving up, please help! I really appreciate your time thank you very much!

Was it helpful?

Solution

Sudo rm -rf had the best response

Well why don't you try it? It's been a while since I've used TBXML. Since then, I've moved on to using libxml2 directly (through a Cocoa wrapper) so I can use X-path to directly select what I want without looping through. As an alternative, you should try this parsing library; I've heard excellent things about its simplicity. github.com/ZaBlanc/RaptureXML I'm sorry that I don't have the time to write a test case for this at the moment.

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