Question

I have an iPhone app that is parsing a WordPress default RSS feed. It is parsing various tags including title, pubDate, dc:creator and link.

dc:creator tag used to be like its written below. The Wordpress sites that i haven't updated still shows this tag as below:

<dc:creator>Andy RR</dc:creator>

I am using NSXMLParser. I was able to successfully get this text using the following code in my didStartElement method:

if ([elementName isEqual:@"dc:creator"])
    {
        currentString = [[NSMutableString alloc] init];
        [self setCreator:currentString];
    }

But with the updated WordPress sites, the default feed that is found under http ://sitename.com/feed shows the dc:creator tag enclosed inside CDATA as follow:

<dc:creator>
<![CDATA[ Andy RR ]]>
</dc:creator>

So the previous code is not getting the text inside dc:creator since its enclosed by CDATA block now.

Now i want to get this CDATA block under dc:creator. I can use the following method of NSXMLParser:

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    currentString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
    NSLog(@"CDaTa text %@", currentString);
}

But the problem is that this method gets all the text from CDATA from other tags as well that are enclosed under CDATA tag. Remember other than dc:creator, there are other tags like "category" and "description" that also have the text enclosed under CDATA block.

What i want is how can the NSXMLParser's foundCDATA method can get only the text under dc:creator tag and ignores other CDATA tags like category and description. How can i achieve that?

Was it helpful?

Solution

Solved it my self by using the following:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
     if ([elementName isEqual:@"dc:creator"])
     {
          currentString = [[NSMutableString alloc] init];
          [self setCreator:currentString];
    }
}

and in the cdata method of NSXMLParser, just appending string worked:

    -(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
    {
          NSString *someString1 = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
         [currentString appendString:someString1];
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top