2
votes
<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>

I want to parse this xml. But it is root name start with News, other element start with also News.
This is what I have tried (without success)

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

How can I parse this type of XML using the NSXMLParser?

2

2 Answers

1
votes

You could check for the existence of 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];
        }
    }
}

Frankly, I'd rather see you fix the XML source, as it's a poor design to use the same element name for different purposes, but if you're stuck with the existing XML structure, something like the above would handle it.

1
votes

You can use the attributes for distinguish them.

Try this code:

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