使用NSXML解析器解析相同的根元素

时间:2013-06-12 13:54:57

标签: objective-c nsxmlparser

<News TotalRecords="10">
<News ID="381222" DateTime="2013-06-12T00:05:00" Title="abc" HeadlineImgSrc="http://www.msa.net/common/news/images/381222/headline.jpg"/>
<News ID="381192" DateTime="2013-06-11T16:46:00" Title="asdsad" HeadlineImgSrc="http://www.asdsad.net/common/news/images/381192/headline.jpg"/>
<News ID="381085" DateTime="2013-06-11T11:34:00" Title="sdsadsadsadsad" HeadlineImgSrc="http://www.asdsad.net/common/news/images/381085/headline.jpg"/>
<News ID="381042" DateTime="2013-06-11T09:16:00" Title="asdsad" HeadlineImgSrc="http://www.asdsad.net/common/news/images/381042/headline.jpg"/>
<News ID="380972" DateTime="2013-06-11T00:01:00" Title="asdsad" HeadlineImgSrc="http://www.asdsad.net/common/news/images/380972/headline.jpg"/>
<News ID="380908" DateTime="2013-06-10T16:23:00" Title="asdsad" HeadlineImgSrc="http://www.asdsad.net/common/news/images/380908/headline.jpg"/>
</News>

我想解析这个xml。但它是以新闻开头的根名,其他元素也以新闻开头 这就是我尝试过的(没有成功)

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    if ([elementName isEqualToString:@"News"]) {
        app.listArray = [[NSMutableArray alloc] init];        
    } else if([elementName isEqualToString:@"News"] ){

        theList = [[List alloc] init];

        theList.ThumbImageURL = [attributeDict objectForKey:@"HeadlineImgSrc"];
        theList.Title = [attributeDict objectForKey:@"Title"];
        theList.URL = [attributeDict objectForKey:@"DateTime"];  
    }
}

如何使用NSXMLParser解析此类型的XML?

2 个答案:

答案 0 :(得分:1)

您可以检查是否存在TotalRecords

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    if ([elementName isEqualToString:@"News"])
    {
        NSString *totalRecords = attributeDict[@"TotalRecords"];

        if (totalRecords) 
        {
            app.listArray = [[NSMutableArray alloc] init];
        } 
        else
        {
            theList = [[List alloc] init];

            theList.ThumbImageURL = [attributeDict objectForKey:@"HeadlineImgSrc"];
            theList.Title = [attributeDict objectForKey:@"Title"];
            theList.URL = [attributeDict objectForKey:@"DateTime"];

            [app.listArray addObject:theList];
        }
    }
}

坦率地说,我宁愿看到你修复XML源代码,因为为不同的目的使用相同的元素名称是一个糟糕的设计,但如果你坚持使用现有的XML结构,那么类似上面的东西会处理它

答案 1 :(得分:1)

您可以使用这些属性来区分它们。

试试这段代码:

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

   if ([attributeDict objectForKey:@"TotalRecords"])
   {
       app.listArray = [[NSMutableArray alloc] init];
   }
   else if([attributeDict objectForKey:@"ID"])
   {

      theList = [[List alloc] init];

      theList.ThumbImageURL = [attributeDict objectForKey:@"HeadlineImgSrc"];
      theList.Title = [attributeDict objectForKey:@"Title"];
      theList.URL = [attributeDict objectForKey:@"DateTime"];
   }
}