【问题标题】:Parser XML with NSXMLParser使用 NSXMLParser 解析 XML
【发布时间】:2010-11-17 22:33:31
【问题描述】:

我有一个关于 xml 解析的问题。通常,XML 文件样式是这样的:

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

这样,我们使用

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

识别元素。

现在,我面临的 xml 文件样式如下:

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

在这种情况下,我们不能使用

elementName isEqualToString:@"name"

所以我尝试使用

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

识别“名称”,但它给了我一个无值,如果我试图获取“名称”的值,什么都没有。

那么,有没有人有任何建议来解决这个问题?谢谢

PS:这是我的部分代码

- (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];
        
        
    }
}

这两个部分运行良好,但如果我尝试获取“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>

有什么建议吗?顺便说一句,整个 xml 文件在这里:ftp://ftp2.bom.gov.au/anon/gen/fwo/IDV10753.xml

【问题讨论】:

  • 你确定你使用的是 NSXMLParser 吗?它是一个 SAX 风格的解析器,这意味着每次它到达一个元素的开头/结尾时都会收到一个回调,等等——没有 API 可以通过名称向它询问一个元素。 NSXML 听起来更像你在说什么。
  • 您发布的一小部分代码看起来不错。问题可能出在其他地方。发布您正在实现的所有 NSXMLParser 委托方法中的所有代码。
  • 是的,我正在使用 NSXMLParser。原始的 XML 并不是这么简单,我只是对这种 XML 样式感到困惑。如果 XML 文件没有 没有问题。
  • 代表很好,我很确定 "if ([elementName isEqualToString:@"element"]) { if ([[attributeDict objectForKey:@"type"] isEqualToString:@" 有问题名称"){ ..... } }
  • 整个代码都很大>.

标签: iphone objective-c xml parsing


【解决方案1】:

我假设您希望将预测期的所有“元素”添加到您将 index 和 start-time-local 放入的同一个字典中?

您在问题中发布的原始基本 if 语句很好,但问题是使用 NSXMLParser,每个 xml 标记(“forecast-period”、“element”、“text”等)都会导致 didStartElement 方法触发.

由于有多个“预测周期”标签和多个“元素”标签,您的代码将需要跟踪当前父标签/对象是什么。处理完“forecast-period”标签后,didStartElement 将再次触发下一个“element”标签。在该调用中,方法参数不会表明此“元素”是“预测周期”的子元素,“索引”为 1。您的代码必须跟踪它。

此外,由于“元素”标签具有实际值(例如,type=precis 的“Sunny”),您必须实现 foundCharacters 和 didEndElement。

在didEndElement中,根据当前目标对象是什么,你必须把数据放在合适的位置。如果它是“预测期”的结束,什么也不做(它没有价值)。如果它是“元素”标签的结尾,则必须获取由 foundCharacters 捕获的值并将其添加到 weatherLoad 中的最后一个字典中。这也意味着您需要改为使用 NSMutableDictionary 而不是 NSDictionary。

@itsaboutcode 为您提供的链接是上述所有内容的一个很好的示例(位于“处理属性”部分的上方。

【讨论】:

    【解决方案2】:

    阅读 Apple Event-Driven XML Programming 中“属性处理”下的部分,您将得到答案。

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

    【讨论】:

    • @David - 正如 aBitObvious 所建议的那样,这是可行的方法,但您可能在其他部分有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 2012-09-02
    • 1970-01-01
    • 2016-02-10
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多