简体   繁体   中英

NSXML parser read attributes

I have a element that is repeating and i need to read it's attributes and send them to the delegate

the xml is:

<special>
   <day date="22/04/2011" name="Easter Friday">Closed</day>
   <day date="23/04/2011" name="Easter Saturday">10:00-16:00</day>
   <day date="24/04/2011" name="Easter Sunday">Closed</day>
   <day date="25/04/2011" name="Anzac Day">13:00-17:00</day>
   <day date="26/04/2011" name="Easter Tuesday">09:00-18:00</day>
</special>

i only get to past the last attributes for date and name to the delegate and i know why is this happening but i dont know how to fix it. can someone help me

here is my objective C code

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"special"]) {
        storeAppDelegate.openingHoursSpecialDelegate = [[NSMutableArray alloc] init];
    }else if ([elementName isEqualToString:@"day"]) {
        openingHoursView = [[OpeningHoursView alloc] init];
        openingHoursView.name = [attributeDict objectForKey:@"name"];
        openingHoursView.date = [attributeDict valueForKey:@"date"];
      }
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"special"])
        return;
    if ([elementName isEqualToString:@"day"]){
        [storeAppDelegate.openingHoursSpecialDelegate addObject:openingHoursView];
        [openingHoursView release];
        openingHoursView = nil;     

    }   
}

openingHoursSpecialDelegate is a mutable array in the app delegate and OpeningHoursView is a NSObject that has name and date as strings in it in another class. They also get the value of the app delegate and it is also only the last read value for "date" and "name" attributes from the XML file . I'm working with NSXML parser so again my question is how to get "openingHoursView.name" and "openingHoursView.date" to write every value they get to openingHoursSpecialDelegate and not overwrite them as it happens now

I can't find anything wrong with the code. I've put the above code into a small test project (with minor changes to make it run standalone), and it runs fine for me.

Array ( "Easter Friday, 22/04/2011", "Easter Saturday, 23/04/2011", "Easter Sunday, 24/04/2011", "Anzac Day, 25/04/2011", "Easter Tuesday, 26/04/2011" )

Example project

You'll need to change the path I've hardcoded in the class test2AppDelegate, to point to a file containing the XML you posted above.

Already i have workout this problem in my project.But i am using libxml2.
The problem is (day node) you have to set the 5 different value to same key (day) thats why you get last attribute .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM