使用NSXMLParser解析XML [英] Parser XML with NSXMLParser

查看:90
本文介绍了使用NSXMLParser解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我对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"){
      .....
   }
}

标识名称",但是它给了我一个none值,如果我尝试获取"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

解决方案

我假设您希望将预测期的所有元素"添加到您放入索引和本地时间起点的同一词典中? /p>

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

由于存在多个预测期"标记和多个元素"标记,因此您的代码将需要跟踪当前的父标记/对象​​是什么.处理完"forecast-period"标签之后,didStartElement将再次为以下"element"标签触发.在该调用中,方法参数将不表示此元素"是带有索引" 1​​的预言期"的子代.您的代码必须对此进行跟踪.

此外,由于"element"标记具有实际值(例如,type = precis为"Sunny"),因此您必须实现foundCharacters和didEndElement.

在didEndElement中,基于当前目标对象是什么,您必须将数据放置在正确的位置.如果它是预测期"的末尾,则不执行任何操作(它没有任何价值).如果它是"element"标记的结尾,则必须获取foundCharacters捕获的值,并将其添加到weatherLoad中的最后一个字典中.这也意味着您需要更改为使用NSMutableDictionary而不是NSDictionary.

@itsaboutcode提供给您的链接具有上述所有示例(位于处理属性"部分上方.

hey guys, 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

解决方案

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.

这篇关于使用NSXMLParser解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆