开发者

iPhone how to get CDATA attribute using NSXMLParser?

I want to parse from an online XML where the XML is like below:

<menu maincategory="![CDATA[Erhverv]]" subcategory="![CDATA[Erhvervsregister]]" category="![CDATA[Akupunktur]]"/>

In the function parser didStartElement i used like below:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if ([elementName isEqualToString:@"menu"]) {
        NSString *maincategory = [attributeDict objectForKey:@"maincategory"];
    }
}

The output is like below using %@:

attributeDict:{
    category = "![CDATA[Advokater]]";
    maincategory = "![CDATA[Erhverv]]";
    subcategory = "![CDATA[Erhvervsregister]]";
}


2011-05-27 19:20:42.663 XML[62494:840b] maincategory using %s: `ãâå
2011-05-27 19:20:42.663 XML[62494:840b] maincategory using %@: ![CDATA[Erhverv]]

But i need the output without the ![CDATA] tag

Can anyone please help me that h开发者_Go百科ow can i solve that? Thanks in advance


I would post process the attributeDict with an NSRegularExpression object. For each string in the dictionary, copy it into an NSMutableString (named theString in my code below) and run this code:

NSError *theError = nil;
NSRegularExpression *theExpression = [NSRegularExpression regularExpressionWithPattern:@"!\\[CDATA\\[(.*)\\]\\]" options:(NSRegularExpressionOptions)nil error:&theError];
NSUInteger count = [theExpression replaceMatchesInString:theString options:(NSRegularExpressionOptions)nil range:NSMakeRange(0, [theString length]) withTemplate:@"$1"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜