The service am trying to parse is here: http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
I am trying to parse a simple xml. The structure of the xml looks something like this:
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
............
............
My parsing code is:
-(void)parserDidStartDocument:(NSXMLParser *)parser{
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Element Name is =%@, \nNamespaceURI is =%@, \nQualifiedName is =%@, \n, Attributes Dictionary is = %@",elementName,namespaceURI,qName,attributeDict);
currentEltValue = elementName;
if([elementName isEqualToString:@"status"]){
parsedata = [[NSString alloc ]init];
NSLog(@"Initializing the variable here");
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([currentEltValue isEqualToString:@"status"]){
NSLog(@"Inside if loop");
parsedata = string;
NSLog(@"Found Characters value is = %@",parsedata);
}
else parsedata = NULL;
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"status"]){
NSLog(@"PARSED DATA: = %@",parsedata);
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"Did end document \t parseData Valus is =%@",parsedata);
}
When i run the program, in my console i am getting:
2012-08-01 13:21:10.721 XML_Parsing[5624:207] Element Name is =status,
NamespaceURI is =(null),
QualifiedName is =(null),
, Attributes Dictionary is = {
}
2012-08-01 13:21:10.722 XML_Parsing[5624:207] Initializing the variable here
2012-08-01 13:21:10.722 XML_Parsing[5624:207] Inside if loop
2012-08-01 13:21:10.723 XML_Parsing[5624:207] Found Characters value is = OK
2012-08-01 13:21:10.723 XML_Parsing[5624:207] PARSED DATA: = OK
2012-08-01 13:21:10.724 XML_Parsing[5624:207] Inside if loop
2012-08-01 13:21:10.724 XML_Parsing[5624:207] Found Characters value is =
2012-08-01 13:21:10.803 XML_Parsing[5624:207] Did end document parseData Valus is =(null)
I have no clue as to why the if condition is running twice?
UPDATE *Solution* This solved my problem:
1] In the DidEndElement: declare currentEltValue = @"".
2] Access all instances of parsedata as self.parsedata.