3

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_minimum" 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

peterh
  • 11,875
  • 18
  • 85
  • 108
david
  • 139
  • 1
  • 4
  • 10
  • Are you sure you're using NSXMLParser? It's a SAX-style parser, meaning you get a callback every time it hits the beginning/end of an element etc -- there is no API to ask it for an element by name. NSXML sounds more like what you're talking about. – Alex Martini Nov 18 '10 at 00:31
  • 1
    The small portion of code you've posted looks fine. The problem may be somewhere else. Post all the code from all the NSXMLParser delegate methods you're implementing. –  Nov 18 '10 at 02:25
  • yes, im using the NSXMLParser. The original XML is not this simple, im just confusing about this kind of XML style. There is no problem if the XML file doesn't have . – david Nov 18 '10 at 03:13
  • delegates are fine, im pretty sure there is something wrong with "if ([elementName isEqualToString:@"element"]) { if ([[attributeDict objectForKey:@"type"] isEqualToString:@"name"){ ..... } } – david Nov 18 '10 at 03:16
  • the whole codes are pretty big >. – david Nov 18 '10 at 03:18
  • Try to create a small, separate project that duplicates the problem (that just tries to see if it can parse that xml and does nothing else). Or, post just the key methods and cut out stuff that you think is definitely not relevant. I assume you've already tried to step through the code in the debugger and/or NSLogged at various points to confirm variables are what they're supposed to be. –  Nov 18 '10 at 03:26
  • @aBitObvious, please look ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml – david Nov 18 '10 at 03:31
  • how could I get this part work"1 12 24 Sunny." – david Nov 18 '10 at 03:32
  • The sample if-statement you posted in the question looks fine. Post at least the code that is in the "..." part that you omitted. Also post the foundCharacters method. Add this code to the question. –  Nov 18 '10 at 03:39
  • I'v updated my question, please have a look, thanks – david Nov 18 '10 at 03:41

2 Answers2

2

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.

0

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

itsaboutcode
  • 24,525
  • 45
  • 110
  • 156