Question

I am completely baffled by this as I have coded several other methods using TBXML to properly parse an XML file. Below is the method in question. The root element remains null no matter what I try therefore the rest of the method fails. As you can see, the XML file is located on the web being output via PHP. I've used this exact same method in another application with no trouble whatsoever. I've compared the XML output to what I previously used and see no differences other than elements. The code for this method is almost identical but I must be missing something. Any help would be greatly appreciated.

- (void)loadFromZenPhoto{
tbxml = [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://www.flpublicarchaeology.org/civilwar/generatexml.php"]];    

// Obtain root element
TBXMLElement *root = tbxml.rootXMLElement;

// if root element is valid
if (root) {
    TBXMLElement *site = [TBXML childElementNamed:@"site" parentElement:root];

    // if a site element was found, create site object
    while (site != nil) {

        Site *aSite = [[Site alloc] init];

        TBXMLElement *siteid = [TBXML childElementNamed:@"id" parentElement:site];
        if (siteid != nil)
            aSite.siteid = [[TBXML textForElement:siteid] intValue];

        TBXMLElement *parentid = [TBXML childElementNamed:@"parentid" parentElement:site];
        if (parentid != nil)
            aSite.parentid = [[TBXML textForElement:parentid] intValue];

        TBXMLElement *folder = [TBXML childElementNamed:@"folder" parentElement:site];
        if (folder != nil)
            aSite.folder = [TBXML textForElement:folder];

        TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:site];
        if (title != nil)
            aSite.title = [TBXML textForElement:title];        


        // add site object to the sites array
        [sites addObject:aSite];

        //advance to next sibling
        site = [TBXML nextSiblingNamed:@"site" searchFromElement:site];
    }           
}
}
Was it helpful?

Solution

Ok, I think you are facing the same problem that I had before. I guess the problem is you fixed the semantic issues in TBXML.m by XCode suggestions. Please this answer for more info.

OTHER TIPS

Well this was a tricky one. It looks like your web service is returning ASCII encoded html. Whereas TBXML expects a UTF8 encoding.

Try checking with this

NSLog(@"stringLoad: %@", [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.flpublicarchaeology.org/civilwar/generatexml.php"] encoding:NSUTF8StringEncoding error:nil]);

It logs to console as stringLoad: (null), but if you use NSLog(@"stringLoad: %@", [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.flpublicarchaeology.org/civilwar/generatexml.php"] encoding:NSASCIIEncoding error:nil]);

the console logs the correct html response.

Hope this helps.

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