Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It appears the CDATA contents in the <code>&lt;fact&gt;</code> tags is being returned incrementally over multiple call backs in <code>parser:foundCharacters</code>. In you class where you are conforming to NSXMLParserDelegate try building up the CDATA by appending it to an NSMutableString instance, like so:</p> <p>(Note: here _currentElement is an NSString property and _factString is an NSMutableString property)</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { self.currentElement = elementName; if ([_currentElement isEqualToString:@"fact"]) { // Make a new mutable string to store the fact string self.factString = [NSMutableString string]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"fact"]) { // If fact string starts with CDATA tags then just get the CDATA without the tags NSString *prefix = @"&lt;![CDATA["; if ([_factString hasPrefix:prefix]) { NSString *cdataString = [_factString substringWithRange:NSMakeRange((prefix.length+1), _factString.length - 3 -(prefix.length+1))]; // Do stuff with CDATA here... NSLog(@"%@", cdataString); // No longer need the fact string so make a new one ready for next XML CDATA self.factString = [NSMutableString string]; } } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if ([_currentElement isEqualToString:@"fact"]) { // If we are at a fact element, append the string // CDATA is returned to this method in more than one go, so build the string up over time [_factString appendString:string]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload