1

I am making a mac application, and I need it to parse a local xml file and display it into a tableView. For some reason, I get a blank row in my tableView, which makes no sense, as it has found characters that are in my xml. Here is my code:

- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"found file and started parsing");
}

- (void)parseXMLFileAtURL:(NSString *)URL
{
    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain

    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];


    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate: self];
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
    [rssParser parse];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSString * errorString = [NSString stringWithFormat:@"Unable to download XML feed (Error code %i )", [parseError code]];
    NSLog(@"Error parsing XML: %@", errorString);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    //NSLog(@"found this element: %@", elementName);
    currentElement = [elementName copy];

    if ([elementName isEqualToString:@"event"]) {
        // clear out our story item caches...
        item = [[NSMutableDictionary alloc] init];
        date = [[NSMutableString alloc] init];
        opponent = [[NSMutableString alloc] init];
        location = [[NSMutableString alloc] init];
        time = [[NSMutableString alloc] init];
        games = [[NSMutableString alloc] init];

    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"ended element: %@", elementName);

    if ([elementName isEqualToString:@"event"]) {
        // save values to an item, then store that item into the array...
        [item setObject:date forKey:@"date"];
        [item setObject:opponent forKey:@"opponent"];
        [item setObject:location forKey:@"location"];
        [item setObject:time forKey:@"time"];
        [item setObject:games forKey:@"games"];
        NSMutableArray *stories = [[NSMutableArray alloc] init];
        [stories addObject:[item copy]];

        [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                    date, @"date", 
                                    opponent, @"opponent",
                                    location, @"location",
                                    time, @"time",
                                    games, @"games"
                                    , nil]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...

    if ([date isEqualToString:@"date"]) {
        [date appendString:string];
    } else if ([opponent isEqualToString:@"opponent"]) {
        [opponent appendString:string];
    } else if ([location isEqualToString:@"location"]) {
        [location appendString:string];
    } else if ([time isEqualToString:@"time"]) {
        [time appendString:string];
    } else if ([games isEqualToString:@"games"]) {
        [games appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"all done!");
    [tabelview reloadData];
}

When i remove my adding part, where it adds item to the arraycontroller, and add

[arrayController addObject:stories]; I get a buch of ('s

If there is anything else you need, do not just down-vote, and instead tell me. Thanks!

Here is my xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
<xmlData>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
    <event>
        <date>date here</date>
        <opponent>opponent here</opponent>
        <location>location here</location>
        <time>time here</time>
        <games>games here</games>
    </event>
</xmlData>
4
  • 3
    -1 for pastebin. Some day your code will be gone from pastebin and this question will become useless. Please include the code and the xml file directly in your question. Commented Feb 3, 2013 at 19:10
  • Ok, sorry, it was just cleaner, i will in a sec Commented Feb 3, 2013 at 19:10
  • OK, i removed pastebin...please do not downvote :) Commented Feb 3, 2013 at 20:37
  • i my case i gt the same thing i.e is the empty table view and it came because i had not initialised the array i had added all the elements into. Commented Feb 7, 2013 at 10:04

2 Answers 2

1

The error is in your parser. Please revise the logic. You are not using your object item when filling your table view array. Also, you are not catching the text in between the XML elements and are not assigning them to the appropriate variables.

Note the following:

  • When you enter an element, keep track of which element you are in currently
  • When you find characters you have to fill the appropriate attribute variable according to which element you are in
  • When you finish the event element, you should add your item with all its filled in keys into your data array.
Sign up to request clarification or add additional context in comments.

6 Comments

Ok, thanks, where exactly in my code did i do something wrong?
What do you mean i am not using item? i am copying it to stories, then adding stories to my tableview, but i still get a blank row...well, 4 rows, for each event.
Sorry, my updated question...also, i still can not seem to fix it. I have set NSlogs everywhere, and can not figure out what is wrong :(
You are adding a new NSDictionary to your arrayController, not item. Slow down and think it through.
Not sure, also, i have still been trying, but i have not figured out the problem...
|
0

You need to know how to use array. You are allocating one array stories. But then not using. Please Check your didEndElement method.

Make one class of Event, create .h and .m file and then create properties of your all element and then add whole object of Event class into an array. That array you can in appHandler or Single ton class.

Check this thing. May it help you.

2 Comments

i have tried saying [arrayController addObject:stories]; , but i get a buch of ('s and sometimes blank spaces
Instead of comment please come here chat.stackoverflow.com/rooms/23864/…

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.