0
votes

I want to parse on xml data from this url

I had written the code like this:-

NSString *escapedURL = @"http://www.google.com/ig/api?weather=%22Dr%20Vikram%20Sarabhai%20Marg%20Panchavati%20Society%20Gulabai%20Tekra%20Ahmedabad%20Gujarat%20India%22&hl=hr";        
            xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:escapedURL]];;
            [xmlParser setDelegate:self];
            [xmlParser setShouldProcessNamespaces:NO];
            [xmlParser setShouldReportNamespacePrefixes:NO];
            [xmlParser setShouldResolveExternalEntities:NO];
            [xmlParser parse];

- (void)parserDidStartDocument:(NSXMLParser *)parser 
{
    NSLog(@"Document started", nil);
    currentElement = nil;
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
{
    NSLog(@"Error: %@", [parseError localizedDescription]);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName 
    attributes:(NSDictionary *)attributeDict
{

    [currentElement release];
    currentElement = [elementName copy];

    currentName = [[NSMutableString alloc] init];

    //For temeprature
    if ([currentElement isEqualToString:@"forecast_information"])
    {


    }
    else if ([currentElement isEqualToString:@"current_conditions"])
    {



    }
    else if ([currentElement isEqualToString:@"temp_c"])
    {
        //NSLog(@"attributeDict==>%@",[attributeDict objectForKey:@"data"]);
        LblTemperature.text=[NSString stringWithFormat:@"Ahmedabad %@°",[attributeDict objectForKey:@"data"]];
    }



}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName
{



     if ([elementName isEqualToString:@"temp_c"])
    {   
        //NSLog(@"temp_c: %@", currentName);

    }




}        

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

    if ([currentElement isEqualToString:@"temp_c"])
    {
        [currentName appendString:string];
        //NSLog(@"==>%@",string);
    }


}

- (void)parserDidEndDocument:(NSXMLParser *)parser 
{
    NSLog(@"Document finished");

}

But I am not able to get the data of temp_c in this parsing. the element name which I am getting is :- xml_api_reply weather forecast_information city postal_code latitude_e6 longitude_e6 forecast_date current_date_time unit_system current_conditions

Please help me..

1
Just telling : You exposed your address in escapedURL.. :-D - rohan-patel
Maybe this parsing method could help you. - rohan-patel
@Thanks for the quick reply.I know that i could solved it by using string operation.But i want to do it in proper way.so i think using nsxmlparser is best.As the string operation is hte last option. - ios developer
Well I use that parsing method in every project and it works without any glitches .. - rohan-patel

1 Answers

1
votes

Well, you may use XPathQuery for parsing the XML. It will make your life much easier. Here is the link for XPathQuery class and a tutorial too.

http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html

Have a look at it, if you are willing to work with it, and still do not understand anything, comment.