0

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.

footyapps27
  • 3,982
  • 2
  • 25
  • 42

2 Answers2

2

Replace your delegate method as follows, You have to set the currentEltValue to empty string when the tag is ended.

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString      *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"status"]){
        NSLog(@"PARSED DATA: = %@",parsedata);
        currentEltValue=@"";
    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

    if([currentEltValue isEqualToString:@"status"]){

        NSLog(@"Inside if loop");
        parsedata = string;
        NSLog(@"Found Characters value is = %@",parsedata);

    }

}
0

According to the docs,

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.

So that would explain why the if condition is executed multiple times. Perhaps the second string is just empty. Try printing the length of each string. Maybe its getting called for another status element.

As to why parseData is NULL, its happening in the else part.

else parsedata = NULL;

Bear in mind that parser:foundCharacters: gets called for other elements also. So when the parser finishes, it would have encountered those elements and parseData would have got assigned to null.

MadhavanRP
  • 2,832
  • 1
  • 20
  • 26
  • i removed the else part still am getting the same output. 2012-08-01 15:17:33.951 XML_Parsing Inside if loop 2012-08-01 15:17:33.952 XML_Parsing Found Characters value is=OK 2012-08-01 15:17:33.953 XML_Parsing[8151:207] PARSED DATA=OK 2012-08-01 15:17:33.954 XML_Parsing[8151:207] Inside if loop 2012-08-01 15:17:33.954 XML_Parsing[8151:207] Found Characters value is 2012-08-01 15:17:33.957 XML_Parsing[8151:207] Did end document parseData Valus is =result And the link is, [link]http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false – footyapps27 Aug 01 '12 at 09:49
  • @footyapps27 How is parseData value "result" which seems to be one of the tags? Are you assigning to parseData anywhere else? – MadhavanRP Aug 01 '12 at 09:56
  • well as u can see from the program, no. That is what am confused about, as to why it is taking one of the tags. – footyapps27 Aug 01 '12 at 10:04
  • @footyapps27 Oh right! try parseData = [NSString stringWithString:string]; instead of parseData = string; – MadhavanRP Aug 01 '12 at 10:18
  • tried that too ya... The problem is why is it again going back to the "if" condition in "foundCharacters". The first time it does assign "OK" to the variable, but second time it goes without going to the "didStartElement". Still cannot figure out as to why it is going from "didEndElement", to "foundCharacters", thereby skipping "didStartElement". – footyapps27 Aug 01 '12 at 10:27
  • @footyapps27 Perhaps it is finding some characters before the result element is recognized. Try printing its size. If you really just want to get OK from it, change parseData to a NSMutableString and just call [parseData appendString:string] on it like the doc suggests as thats how its meant to be used. Looks like it would solve your problem. – MadhavanRP Aug 01 '12 at 10:44
  • append would help if you just have one tag, but suppose you want to try the same thing where you have three tags with the same name. There the problem still persists. – footyapps27 Aug 01 '12 at 10:57