我有一个应用程序,我使用nsxmlparser来解析XML。但是我没有XML文件。我有一个生成XML文件的API方法。我在控制台中生成了xml文件。但问题是我不知道如何解析API方法。我编写了以下代码,但它不会产生输出。
TWeatherElement.h
//This is my element class .
#import <Foundation/Foundation.h>
@interface TWeatherElement : NSObject
{
NSString *mIcon;
NSString *mCurrentdate;
NSString *mConditionname;
NSString *mMintemp;
NSString *mMaxtemp;
NSString *mWind;
NSString *mHumidity;
}
@property (nonatomic,retain) NSString *icon;
@property (nonatomic, retain)NSString *currentdate;
@property (nonatomic,retain)NSString *conditionname;
@property (nonatomic,retain)NSString *mintemp;
@property (nonatomic, retain)NSString *maxtemp;
@property (nonatomic, retain)NSString *wind;
@property (nonatomic, retain)NSString *humidity;
@end
TWeatherElement.m
#import "TWeatherElement.h"
@implementation TWeatherElement
@synthesize icon = mIcon;
@synthesize currentdate = mCurrentdate;
@synthesize conditionname = mConditionname;
@synthesize mintemp = mMintemp;
@synthesize maxtemp = mMaxtemp;
@synthesize wind = mWind;
@synthesize humidity = mHumidity;
-(void)dealloc
{
[mIcon release];
//[mForecastdate release];
[mCurrentdate release];
[mConditionname release];
[mMintemp release];
[mMaxtemp release];
[mWind release];
[mHumidity release];
[super dealloc];
}
@end
TWeatherParser.h
//TWeatherParser is my parser class
#import "TWeatherElement.h"
#import <Foundation/Foundation.h>
@interface TWeatherParser : NSObject<NSXMLParserDelegate>
{
NSMutableArray *mParserArray;
NSXMLParser *mXmlParser;
NSMutableString *mCurrentElement;
BOOL elementFound;
TWeatherElement *mWeather;
}
@property (nonatomic, retain) NSMutableString *currentElement;
@property (nonatomic, retain) TWeatherElement *weatherobj;
-(void)getInitialiseWithData:(NSData *)inData;
@end
TWeatherParser.m
#import "TWeatherParser.h"
#import "JourneyAppDelegate.h"
//#define kParsingFinishedNotification @"ParsingFinishedNotification"
@implementation TWeatherParser
@synthesize weatherobj = mWeatherobj;
@synthesize currentElement = mCurrentElement;
-(void)getInitialiseWithData:(NSData *)inData
{
mWeather = [[TWeather alloc]init];
[mParserArray removeAllObjects];
NSBundle* bundle = [NSBundle mainBundle];
//This is to get to the path of the xml file named contact_data, but
//my problem is I don't have an XML file. My XML file is generated
//through an API method, [(NSString *)getBusXMLAtStop:(NSString*)stopnumber]
What should I write here so that I can parse my XML file which
is retrieving through the above below API method?
}
API METHOD:
-(NSString *)getBusXMLAtStop:(NSString*)stopnumber
{
NSError *error;
NSURLResponse *response;
NSData *dataReply;
NSString *stringReply;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString: [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=,,,50500000,30500000",stopnumber]]];
[request setHTTPMethod: @"GET"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//NSString *str;
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
NSLog(@"%@",stringReply);
return stringReply;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString*) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString*)qualifiedName attribute:(NSDictionary*)attributeDict
{
if (nil!= qualifiedName)
{
elementName = qualifiedName;
}
if ([elementName isEqualToString:@"weather"])
{
self.weatherobj = [[TWeatherElement alloc]init];
}
else if([elementName isEqualToString:@"current_date_time data"]||
[elementName isEqualToString:@"condition data"]||
[elementName isEqualToString:@"humidity data"]||
[elementName isEqualToString:@"icon data"]||
[elementName isEqualToString:@"wind_condition data"]||
[elementName isEqualToString:@"low data"]||
[elementName isEqualToString:@"high data"])
{
self.currentElement = [NSMutableString string];
}
else
{
self.currentElement = nil;
}
}
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string
{
if (nil!= self.currentElement)
{
[self.currentElement appendString:string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName
{
if (nil != qName)
{
elementName = qName;
}
if ([elementName isEqualToString:@"current_date_time data"])
{
self.weatherobj.currentdate = self.currentElement;
}
else if ([elementName isEqualToString:@"condition data"])
{
self.weatherobj.conditionname = self.currentElement;
}
else if ([elementName isEqualToString:@"humidity data"])
{
self.weatherobj.humidity = self.currentElement;
}
else if ([elementName isEqualToString:@"icon data"])
{
self.weatherobj.icon = self.currentElement;
}
else if ([elementName isEqualToString:@"wind_condition data"])
{
self.weatherobj.wind = self.currentElement;
}
else if ([elementName isEqualToString:@"low data"])
{
self.weatherobj.mintemp = self.currentElement;
}
else if ([elementName isEqualToString:@"high data"])
{
self.weatherobj.maxtemp = self.currentElement;
}
else if ([elementName isEqualToString:@"weather"])
{
[mParserArray addObject:self.weatherobj];
NSLog(@"mDataArray count = %d",[mParserArray count]);
[self.weatherobj release];
}
}
-(void)dealloc
{
self.weatherobj = nil;
self.currentElement = nil;
[super dealloc];
}
@end
如何在getInitialiseWithData
函数中调用API方法以便我可以解析我的XML文件?
答案 0 :(得分:0)
您无法使用+(id)dataWithContentsOfURL:(NSURL *)aURL
代替contentsOfFile:
答案 1 :(得分:0)
从API获取包含XML的字符串并传递字符串(它必须有一个带字符串的函数)。
或者只是将字符串转换为NSData,
NSData* data=[stringContainingXML dataUsingEncoding:NSUTF8StringEncoding];
您需要使用解析器库,libxml,touchxml或NSXMLparser。在Google上查看,并做一个简单的教程。在那之后你应该没有问题。 我建议touchxml。我从来没有使用它,但我听说它与其他产品相比很容易使用。
祝你好运。