0

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!

Year3000
  • 459
  • 2
  • 7
  • 15
  • What specific data from this xml are you trying to put into your table view? – sudo rm -rf Apr 28 '12 at 14:23
  • From event, I'm trying to pull the uri & displayName. From location the city, lng, & lat. & from start, the time & date. I'm also trying to assign my row count to the number of totalEntries but can't seem to figure that one out. – Year3000 Apr 28 '12 at 19:47
  • I was thinking of something along these lines but I'm not to sure: 'TBXMLElement *event = [TBXML childElementNamed:@"event" parentElement:element]; NSString *uri = [TBXML valueOfAttributeNamed:@"uri" forElement:event]; NSString *venue = [TBXML valueOfAttributeNamed:@"displayName" forElement:event]; [events addObject:[NSArray arrayWithObjects: [TBXML textForElement:event], uri,venue,nil]]; – Year3000 Apr 28 '12 at 20:15
  • 1
    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. https://github.com/ZaBlanc/RaptureXML I'm sorry that I don't have the time to write a test case for this at the moment. – sudo rm -rf Apr 28 '12 at 21:37
  • Cool I'll give that a try when I get home. Do you recall how to assign the row count with tbxml? – Year3000 Apr 28 '12 at 21:43
  • Well if you stuff all this into an array, then you can just return `[array count]` in the tableview delegate that asks for the cell count. – sudo rm -rf Apr 28 '12 at 22:16
  • So I tried using rapture xml and it is much easier. I managed to pull in the info and each attribute that I needed. The only problem is that it's really slow. There's a noticeable delay when I press the tab bar button for the view with my table in it. I'll open up a new question concerning rapture. [link]http://stackoverflow.com/questions/10370153/rapturexml-kind-of-slow – Year3000 Apr 29 '12 at 06:16

1 Answers1

0

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.

Year3000
  • 459
  • 2
  • 7
  • 15