All pastes #1404334 Raw Edit

Shoaibi

public text v1 · immutable
#1404334 ·published 2009-04-27 02:00 UTC
rendered paste body
/****


In response to my own question at: http://stackoverflow.com/questions/773651/xml-parsing-in-cocoa-touch-iphone


SAMPLE XML:

<?xml version="1.0" encoding="UTF-8"?>
<AllEntries>
	<entry>
		<plantID>460</plantID>
		<scientific>Alcea rosea 'Nigra'</scientific>
		<common>Black Hollyhock</common>
	</entry>
	<entry>
		<plantID>466</plantID>
		<scientific>Amaranthus caudatus </scientific>
		<common>love-lies-bleedingLove Lies Bleeding</common>
	</entry>
	<entry>
		<plantID>36428</plantID>
		<scientific>Podocarpus macrophyllus 'Select Spreader'</scientific>
		<common>Japanese Yew 'Select Spreader'</common>
	</entry>
	<entry>
		<plantID>36502</plantID>
		<scientific>Spirea japonica 'limemound'</scientific>
		<common>Spirea Japonica 'Limemound'</common>
	</entry>
</AllEntries>



// See \n or \n\t in parse Array? make sure there are no "manually inserted" line breaks or tabs.
*****/


- (NSMutableArray *) parseXMLWithStringData:(NSString *) Data {		
	
	//NSLog(@"Starting XML Parsing");
	CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithXMLString:Data options:0 error:nil];
	
	
	
	
	NSArray *resultNodes=nil;
	NSMutableArray * Entries = [NSMutableArray new];
	NSMutableDictionary *Item = [[NSMutableDictionary alloc] init];
	resultNodes = [xmlDoc nodesForXPath:@"//entry" error:nil];
	//NSLog(@"print nodes:%@", resultNodes);
	
	
	
	// Loop through the resultNodes to access each items actual data
	for (CXMLElement *resultElement in resultNodes) {		
		int counter;	
		
		//Get the child notes...
		for(counter = 0; counter < [resultElement childCount]; counter++) {
			
			if ([[[resultElement childAtIndex:counter] stringValue] isNotEqualTo:nil])
			[Item  setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
			else
				[Item  setObject:@"!~~~!" forKey:[[resultElement childAtIndex:counter] name]]; // !~~~! for nothing, nil, null
				
		}
		[Entries addObject:[Item copy]];
		
	}
	
	
	
	[resultNodes release];
	[Item release];
	[Entries autorelease];
	[xmlDoc release];
	
	
	//NSLog(@"Printing Entries: %@", Entries);
	return Entries;
}