0

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 how can i solve that? Thanks in advance

itsazzad
  • 6,868
  • 7
  • 69
  • 89
  • Possible dupe of [Reading CData Sections parsing xml in objective-c](http://stackoverflow.com/questions/4851965/reading-cdata-sections-parsing-xml-in-objective-c) – Rayfleck May 27 '11 at 13:46
  • THANKS FOR YOUR SUGGESTION BUT THATS FOR NODE VALUE AND I'M IN NEED OF NODE ![CDATA] PROPERTY IN STRING – itsazzad May 27 '11 at 13:54
  • Although not a direct solution; it would be very easy to simply take "![CDATA[" and "]]" off those strings. – Jake May 27 '11 at 14:14
  • 1
    No need to shout. Have you tried NSString *maincategory = [[attributeDict objectForKey:@"maincategory"] stringByReplacingOccurrencesOfString:@"![CDATA[" withString:@""]; You'll also need to replace the trailing ']'. – Rayfleck May 27 '11 at 14:15
  • Hi Rayfleck, That works but the problem is that it prints correctly in log using %@ but not using %s. using %s its showing `õâ» – itsazzad May 27 '11 at 14:48

1 Answers1

0

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"];
Mr. Berna
  • 10,525
  • 1
  • 39
  • 42