Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're correct that you need to implement <code>didStartElement</code>, <code>foundCharacters</code> and <code>didEndElement</code>. You're also correct that <code>foundCharacters</code> will be called several times in the course of processing a given element, so you need to handle this by <em>appending</em> the characters to a string, rather than creating a new standalone string every time. Or, as Apple puts it in the <a href="http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html" rel="nofollow">docs</a>:</p> <blockquote> <p>The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element. Because string may be only part of the total character content for the current element, you should append it to the current accumulation of characters until the element changes.</p> </blockquote> <p>As an example, here's my complete implementation of a simple working parser from my current project. Note how simple <code>foundCharacters</code> is - all you need to do at the basic level is keep appending the characters to a string until <code>didEndElement</code> is called (then you can clear it within <code>didEndElement</code> and start the appending process again):</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ if([elementName isEqualToString:@"device"]){ self.currentDeviceID = [[SRDeviceID alloc]init]; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if([elementName isEqualToString:@"deviceList"]){ [self.pairingTableView reloadData]; } if([elementName isEqualToString:@"device"]){ [self.deviceIDListFromServer addObject:self.currentDeviceID]; } //The accumulation of characters for the current element is complete, //so we can tidy up the string and use the result: NSString* finalParsedString = [self.currentParseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if([elementName isEqualToString:@"deviceName"]){ self.currentDeviceID.deviceName = finalParsedString; } if([elementName isEqualToString:@"UUID"]){ self.currentDeviceID.uuid = finalParsedString; } if([elementName isEqualToString:@"deviceLocation"]){ self.currentDeviceID.location = finalParsedString; } if([elementName isEqualToString:@"deviceLastSeenTime"]){ self.currentDeviceID.lastSeenTime = finalParsedString; } //Now we've used the accumulated string from the current element, //we can clear it ready to start the accumulation process for //the next element's contents: self.currentParseString = [NSMutableString stringWithString:@""]; } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ //All we need to do here is add the next lot of characters //to the accumulating string for the current element, //which continues to happen until didEndElement is called: [self.currentParseString appendString:string]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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