0

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..

Manali
  • 577
  • 4
  • 11
ios developer
  • 3,363
  • 3
  • 51
  • 111
  • *Just telling : You exposed your address in escapedURL..* :-D – rohan-patel Apr 13 '12 at 12:57
  • Maybe [this parsing method](http://stackoverflow.com/questions/9747396/how-to-parse-xml-file-from-a-server/9747441#9747441) could help you. – rohan-patel Apr 13 '12 at 13:00
  • @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 Apr 13 '12 at 13:06
  • Well I use that parsing method in every project and it works without any glitches .. – rohan-patel Apr 13 '12 at 13:08

1 Answers1

1

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.

Haris Hussain
  • 2,531
  • 3
  • 25
  • 38
  • Thanks for quick reply.is is not possible to do it by Nsxmlparsing. – ios developer Apr 13 '12 at 13:01
  • It is, but its a hassle for sure.. :S I always use XPathQuery for parsing. Have you tried it? Its really simple to use. Just import the classes and use NSArray *PerformXMLXPathQuery(NSData *document, NSString *query); Pass the arguments, have your XML in a NSData object, and for Query Part, pass "//" for root, pass "//root//item" for sub-tree elements. Try NSLog the output, you'll see your desired element/attribute. Then from there on, it wont be a problem for you. – Haris Hussain Apr 13 '12 at 13:09
  • And it will give you the parsed data the 'Proper Way' :) – Haris Hussain Apr 13 '12 at 13:12