问题
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 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>
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
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];
}
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;
}
.
.
.
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];
and didEnd . . .
if([elementName isEqualToString:@"Date"]){
NSLog(@"mutableAcquiredDate : %@", self.mutableAcquiredDate); // this gives the right data
return;
}
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;
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.
How can I reach only the content of the Date node from viewController without having all the contents of the nodes listed. Thanks...
回答1:
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;
and use the delegates like this
@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;
}
}
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
来源:https://stackoverflow.com/questions/15710808/how-to-call-content-of-a-parsed-nsxmlparser-xml-from-another-class-code-revie