I am making a little weather app using the Yahoo api. This api gives me an XML file back. My problem is now how I can parse this file ? Here is my code that I have so far.
我正在用Yahoo api做一个天气应用。这个api给了我一个XML文件。我现在的问题是如何解析这个文件?这是我目前的代码。
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
NSLog(@"success");
XMLParser.delegate = self;
[XMLParser parse];
NSLog(@"xmlParser is %@",XMLParser);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
NSLog(@"failure with error %@",error);
}];
You can find the XML file over here.
可以在这里找到XML文件。
Hope that anybody can help me
希望有人能帮助我
2 个解决方案
#1
6
If you are looking to parse XMLs using NSXMLParser you'll need to have a class that implements the NSXMLParserDelegate. You can use your ViewController for this:
如果要使用NSXMLParser解析XMLs,就需要有一个实现NSXMLParserDelegate的类。你可以使用你的视图控制器
@interface ViewController : UIViewController <NSXMLParserDelegate>
Then using the SAX methods provided by this protocol you can parse this XML when you run [XMLParser parse]
. Here is an example for your xml:
然后使用此协议提供的SAX方法,您可以在运行[XMLParser parse]时解析此XML。下面是xml的一个示例:
- (IBAction) makeRequest:(id)sender
{
NSLog(@"Making request");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://weather.yahooapis.com/forecastrss?w=2442047&u=c"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
XMLParser.delegate = self;
[XMLParser parse];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
NSLog(@"failure with error %@",error);
}];
[operation start];
}
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"yweather:astronomy"])
{
NSLog(@"Sunrise: %@, Sunset: %@", [attributeDict valueForKey:@"sunrise"], [attributeDict valueForKey:@"sunset"]);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
self.tmpInnerTagText = string; // Make a temp NSString to store the text in-between tags
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"title"])
{
NSLog(@"%@", self.tmpInnerTagText);
}
if ([elementName isEqualToString:@"description"])
{
NSLog(@"%@", self.tmpInnerTagText);
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Paser Error = %@", parseError);
//TODO: Create Alert message error
}
Also to get AFNetworking to support the RSS xml you are using I had to add "application/rss+xml" to the acceptableContentTypes following the instructions from this site: http://www.suushmedia.com/simple-rss-reader-with-afnetworking/
另外,为了获得AFNetworking来支持您正在使用的RSS xml,我必须按照这个站点的说明向acceptableContentTypes添加“application/ RSS +xml”:http://www.suushmedia.com/simple-web-ers -reader-with-afnetworking/
Hope this helps
希望这有助于
#2
0
Better Solution: I found here
更好的解决办法:我在这里找到的
NSPropertyListFormat format;
NSArray *myObjects = [NSPropertyListSerialization propertyListWithData:responseObject options:NSPropertyListMutableContainers format:&format error:NULL];
//If the root object of the plist is dictionary
NSDictionary *myObjects = [NSPropertyListSerialization propertyListWithData:responseObject options:NSPropertyListMutableContainers format:&format error:NULL];
#1
6
If you are looking to parse XMLs using NSXMLParser you'll need to have a class that implements the NSXMLParserDelegate. You can use your ViewController for this:
如果要使用NSXMLParser解析XMLs,就需要有一个实现NSXMLParserDelegate的类。你可以使用你的视图控制器
@interface ViewController : UIViewController <NSXMLParserDelegate>
Then using the SAX methods provided by this protocol you can parse this XML when you run [XMLParser parse]
. Here is an example for your xml:
然后使用此协议提供的SAX方法,您可以在运行[XMLParser parse]时解析此XML。下面是xml的一个示例:
- (IBAction) makeRequest:(id)sender
{
NSLog(@"Making request");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://weather.yahooapis.com/forecastrss?w=2442047&u=c"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
XMLParser.delegate = self;
[XMLParser parse];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
NSLog(@"failure with error %@",error);
}];
[operation start];
}
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"yweather:astronomy"])
{
NSLog(@"Sunrise: %@, Sunset: %@", [attributeDict valueForKey:@"sunrise"], [attributeDict valueForKey:@"sunset"]);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
self.tmpInnerTagText = string; // Make a temp NSString to store the text in-between tags
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"title"])
{
NSLog(@"%@", self.tmpInnerTagText);
}
if ([elementName isEqualToString:@"description"])
{
NSLog(@"%@", self.tmpInnerTagText);
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Paser Error = %@", parseError);
//TODO: Create Alert message error
}
Also to get AFNetworking to support the RSS xml you are using I had to add "application/rss+xml" to the acceptableContentTypes following the instructions from this site: http://www.suushmedia.com/simple-rss-reader-with-afnetworking/
另外,为了获得AFNetworking来支持您正在使用的RSS xml,我必须按照这个站点的说明向acceptableContentTypes添加“application/ RSS +xml”:http://www.suushmedia.com/simple-web-ers -reader-with-afnetworking/
Hope this helps
希望这有助于
#2
0
Better Solution: I found here
更好的解决办法:我在这里找到的
NSPropertyListFormat format;
NSArray *myObjects = [NSPropertyListSerialization propertyListWithData:responseObject options:NSPropertyListMutableContainers format:&format error:NULL];
//If the root object of the plist is dictionary
NSDictionary *myObjects = [NSPropertyListSerialization propertyListWithData:responseObject options:NSPropertyListMutableContainers format:&format error:NULL];