如何从另一个类调用已解析(NSXMLParser)xml 的内容?代码审查? [英] How to call content of a parsed (NSXMLParser) xml from another class? Code review?
问题描述
我正在尝试在视图控制器中提供已解析的 xml 文件的内容.当节点字符串的内容为.
I am trying to give the contents of a parsed xml file in the view Controller. How can i do it when the content of the node string is.
xml 文件是这样的:
xml file is like this:
<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 文件中的我的解析器
My Parser in .h file
@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
文件:
-(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...
Delegate methods in .m file like: 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;
}
.
.
.
找到的字符:
[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 文件中
in viewController .h file
@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 文件中
in viewController .m file
// 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 节点的内容.谢谢...
How can I reach only the content of the Date node from viewController without having all the contents of the nodes listed. Thanks...
推荐答案
我找到了将它们放入 NSMutableDictionary
的最简单方法,因为您可以通过它们的原始标签访问它们.在解析器的标头中,声明
I found the easiest way to put them in an NSMutableDictionary
since you could access them by their original tags. In your header for the parser, declare
@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
,并通过从不同的类调用它来访问某些对象,例如日期
This inserts them all into the NSMutableDictionary *dic
according to their tags in the XML, and access certain objects, like the date, by calling this from a different class
[parser.dic objectForKey:@"Date"];
这也会为具有多个同名节点的文件创建一个数组
This also creates an array for a file that has multiple nodes with the same name
这篇关于如何从另一个类调用已解析(NSXMLParser)xml 的内容?代码审查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!