开发者

Parser XML with NSXMLParser

I got a question with xml parsing. Normally,the XML file style like this:

<person>
     <name> abc </name>
     <age> 19 </age>
     <gender> male </gender> 
</person>

In this way, we use

elementName isEqualToString:@"name"
elementName isEqualToString:@"age"
elementName isEqualToString:@"gender"

to identify the element.

Now, I'm facing a xml file style like:

<person>
     <element type="name">abc</element>
     <element type="age">19</element>
     <element type="gender">male</element> 
</person>

In this situation, we can't use

elementName isEqualToString:@"name"

So I tried to use

if ([elementName isEqualToString:@"element"]) {
   if ([[attributeDict objectForKey:@"type"] isEqualToString:@"name"){
      .....
   }
}

to identify the "name", but it gave me a none value, there is nothing if i tried to get the value of "name".

So, does anyone have any suggestions to fix this problem? thanks

PS: Here are part of my code

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict {
    
    
    if ( [elementName isEqualToString:@"area"]) {
        if ([[attributeDict objectForKey:@"type"] isEqualToString:@"location"]) {
            NSString *description = [attributeDict objectForKey:@"description"];
            
            NSArray *lineEntry = [[NSArray alloc] initWithObjects:description,nil ];
            NSArray *lineKeys = [[NSArray alloc] initWithObjects:@"description",nil];
            NSDictionary *dictItem = [[NSDictionary alloc] initWithObjects:lineEntry forKeys:lineKeys];
            
            [lineKeys release];
            [lineEntry release];
            
            [dataLoad addObject:dictItem];
            [dictItem release];
        }
        
    }
    
    if ([elementName isEqualToString:@"forecast-period"]) {
        
        NSString *index = [attributeDict objectForKey:@"index"];
        NSString *time = [attributeDict objectForKey:@"start-time-local"];
        

        NSArray *indexEntry = [[NSArray alloc] initWithObjects:index,time,nil];
        NSArray *indexKey = [[NSArray alloc] initWithObjects:@"index",@"start-time-local",nil];

        NSDictionary *dicIndex = [[NSDictionary alloc] initWithObjects:indexEntry forKeys:indexKey];
        
        [indexEntry release];
        [indexKey   release];
        
        [weatherLoad addObject:dicIndex];
        [dicIndex release];
        
        
    }
}

it worked fine with those 2 parts, but it doesn't work if i try to get "forecast_icon_code","air_temperature_minimum","air_temperature_maximum","precis".

<forecast-period index="1" start-time-local="2010-11-19T00:00:00+11:00" end-time-local="2010-11-20T00:00:00+11:00" start-time-utc="2010-11-18T13:00:00Z" end-time-utc="2010-11-19T13:00:00Z">
<element type="forecast_icon_code">1</element>
<element type="air_temperature_m开发者_开发问答inimum" units="Celsius">12</element>
<element type="air_temperature_maximum" units="Celsius">24</element>
<text type="precis">Sunny.</text>
</forecast-period>

Any suggestions? BTW, the whole xml file is here: ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml


I assume you want all the "elements" of a forecast-period to be added to the same dictionary you put index and start-time-local in?

Your original basic if-statement posted in the question is fine but the problem is that with the NSXMLParser, each xml tag ("forecast-period", "element", "text", etc) causes the didStartElement method to fire.

Since there are multiple "forecast-period" tags and multiple "element" tags, your code will need to keep track of what the current parent tag/object is. After the "forecast-period" tag is processed, didStartElement will fire again for the following "element" tag. In that call, the method parameters will have no indication that this "element" is a child of "forecast-period" with "index" 1. Your code has to keep track of that.

Also, since the "element" tags have actual values (eg. "Sunny" for type=precis), you'll have to implement foundCharacters and didEndElement.

In didEndElement, based on what the current target object is, you have to put the data in the proper place. If it's the end of "forecast-period", do nothing (it has no value). If it's the end of an "element" tag, you have to take the value captured by foundCharacters and add it to the last dictionary in weatherLoad. This also means you need to change to using an NSMutableDictionary instead of an NSDictionary.

The link that @itsaboutcode gave you has a good example of all the above (it's above the "Handling Attributes" section.


Reading the section under "Attribute Handling" in Apple Event-Driven XML Programming and you will have your answer.

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html#//apple_ref/doc/uid/20002265-1001969

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜