Graphics Programs Reference
In-Depth Information
{
NSString *xmlCheck = [[NSString alloc] initWithData:xmlData
encoding:NSUTF8StringEncoding];
NSLog(@"xmlCheck = %@", xmlCheck);
// Create the parser object with the data received from the web service
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
// Give it a delegate - ignore the warning here for now
[parser setDelegate:self];
// Tell it to start parsing - the document will be parsed and
// the delegate of NSXMLParser will get all of its delegate messages
// sent to it before this line finishes execution - it is blocking
[parser parse];
// Get rid of the XML data as we no longer need it
xmlData = nil;
// Get rid of the connection, no longer need it
connection = nil;
// Reload the table.. for now, the table will be empty.
[[self tableView] reloadData];
}
The delegate of the parser, ListViewController , will receive a message when the
parser finds a new element, another message when it finds a string within an element, and
another when an element is closed.
For example, if a parser saw this XML:
<title>Big Nerd Ranch</title>.
it would send its delegate three consecutive messages: “I found a new element: 'title',”
then, “I found a string: 'Big Nerd Ranch',” and finally, “I found the end of an element:
'title'.” These messages are found in the NSXMLParserDelegate protocol:
// The "I found a new element" message
- (void)parser:(NSXMLParser *)parser
// Parser that is sending message
didStartElement:(NSString *)elementName
// Name of the element found
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict;
// The "I found a string" message
- (void)parser:(NSXMLParser *)parser // Parser that is sending message
foundCharacters:(NSString *)string; // Contents of element (string)
// The "I found the end of an element" message
- (void)parser:(NSXMLParser *)parser
// Parser that is sending message
didEndElement:(NSString *)elementName
// Name of the element found
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName;
The namespaceURI , qualifiedName , and attributes arguments are for more
complex XML, and we'll return to them at the end of the chapter.
 
Search WWH ::




Custom Search