我试图在视图控制器中提供已解析的xml文件的内容。如何在节点字符串的内容中执行此操作。
xml文件是这样的:
<XML>
<Data>
<ID_Number>8</ID_Number>
<Date> 13.12.2009</Date>
<Time> 17:50:00</Time>
<Temperature> 37.2 </Temperature>
<Speed>34.2</Speed>
<Direction>90.4</Direction>
<AirPressure>1040.3</AirPressure>
</Data>
</XML>
我的解析器在.h文件中
@interface Parser : NSObject
@property ( nonatomic,strong) NSMutableString *mutableTemperatureValue;
@property ( nonatomic,strong) NSMutableString *mutableWindDirection;
@property ( nonatomic,strong) NSMutableString *mutableWindSpeed;
@property (nonatomic,strong) NSMutableString *mutableAirPressure;
@property (nonatomic,strong) NSMutableString *mutableAcquiredDate;
@property (nonatomic,strong) NSMutableString *mutableAcquiredTime;
-(void)parserStart;
@end
m.file:
-(void)parserStart
{
NSURL *url = [NSURL URLWithString:myWebServiceXMLUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
NSXMLParser *xmlParser = [[NSXMLParser alloc]initWithData:data ];
NSString *xmlCheck = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"xmlCheck =%@", xmlCheck);
[xmlParser setDelegate:self];
[xmlParser parse];
}
在.m文件中委托方法,如: didStart ...
if([elementName isEqualToString:@"Date"] ){
self.mutableAcquiredDate = [[NSMutableString alloc]init];
return;
}
if([elementName isEqualToString:@"Time"]){
self.mutableAcquiredTime = [[NSMutableString alloc]init];
return;
}
if([elementName isEqualToString:@"Temperature"]){
self.mutableTemperatureValue = [[NSMutableString alloc]init];
return;
}
.
.
.
foundCharacters:
[self.mutableTemperatureValue appendString:string];
[self.mutableWindSpeed appendString:string];
[self.mutableWindDirection appendString:string];
[self.mutableAirPressure appendString:string];
[self.mutableAcquiredDate appendString:string];
[self.mutableAcquiredTime appendString:string];
并且完成了 。 。
if([elementName isEqualToString:@"Date"]){
NSLog(@"mutableAcquiredDate : %@", self.mutableAcquiredDate); // this gives the right data
return;
}
在viewController .h文件中
@property (strong, nonatomic) IBOutlet UILabel *labelWindSpeed;
@property (strong, nonatomic) IBOutlet UILabel *labelTemperatur;
@property (strong, nonatomic) IBOutlet UILabel *labelAirPressure;
@property (strong, nonatomic) IBOutlet UILabel *labelUpdateDateTime;
viewController .m文件中的
// Create Parser object and start parsing
self.parser = [[Parser alloc]init];
[self.parser parserStart];
// Assigning the values from Parser Class to labels
// Date and Time
self.labelUpdateDateTime.text = self.parser.mutableAcquiredDate ;
NSLog(@"---------:%@", self.labelUpdateDateTime.text);// This gives all the contents of the xml nodes why?
// Temperatur
myStartTemperatureValue = [self.parser.mutableTemperatureValue floatValue];
labelTemperatur.text = [NSString stringWithFormat:@"%.1f", myStartTemperatureValue];// This works also correct.
如何从viewController只访问Date节点的内容,而不必列出所有节点的内容。 感谢...
答案 0 :(得分:1)
我找到了将它们放在NSMutableDictionary
中的最简单方法,因为您可以通过原始代码访问它们。在解析器的标头中,声明
@property (strong, nonatomic) NSMutableDictionary *dic;
BOOL parsing;
NSString *title;
NSString *cont;
并使用像这样的代表
@synthesize dic;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
parsing = YES;
title = elementName;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (parsing) {
NSMutableString *str1 = [[NSMutableString alloc] initWithCapacity:1000];
[str1 appendString:string];
if ([dic valueForKey:title] == nil) {
[dic setValue:str1 forKey:title];
}
else{
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSMutableArray *delete = [[NSMutableArray alloc] init];
if ([[dic objectForKey:title] isKindOfClass:[NSMutableArray class]]){
for (NSString *str in [dic objectForKey:title]){
[arr addObject:str];
}
[arr addObject:str1];
for (NSString *s in arr) {
if ([s isEqualToString:@"\n"]) {
[delete addObject:s];
}
}
[arr removeObjectsInArray:delete];
[dic setObject:arr forKey:title];
}
else{
[arr addObject:[dic objectForKey:title]];
[arr addObject:str1];
for (NSString *s in arr) {
if ([s isEqualToString:@"\n"]) {
[delete addObject:s];
}
}
[arr removeObjectsInArray:delete];
[dic setObject:arr forKey:title];
}
}
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if (parsing) {
parsing = NO;
}
}
根据XML中的标记将它们全部插入到NSMutableDictionary *dic
中,并通过从不同的类中调用它来访问某些对象(如日期)
[parser.dic objectForKey:@"Date"];
这也会为具有多个同名节点的文件创建一个数组