如何阅读此XML的内容?
<Locations>
<Location>4</Location>
</Locations>
我可以解析它,它可以工作,但我无法访问Location
(4
)的值。我正在使用:
ZoneCheck *azone;
NSString *url1 = [NSString stringWithFormat:@"%@", azone.location];
答案 0 :(得分:0)
我已经发布了答案,请点击以下链接“https://stackoverflow.com/questions/10877374/how-to-parse-the-attributes-like-in-this-xml-file-through-xml-parser/10877758#10877758'
首先拿一个bool变量。然后编写以下代码。也许它会帮助你我几天前完成了相关的工作。
首先.h文件
@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
NSMutableArray *_locationArray;
BOOL isLocation;
}
然后在.m文件中你应该写这个。
//在ViewDidLoad中
- (void)viewDidLoad
{
[super viewDidLoad];
_locationArray = [[NSMutableArray alloc] init];
NSURL * fileURL = [NSURL fileURLWithPath:xmlString];
NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
[parser setDelegate:self];
[parser parse];
}
//现在在xml Parser委托方法中 编译指示标记 - NSXMLParser委派方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Locations"]) {
isLocation = YES;
}
else if ([elementName isEqualToString:@"Location"] && isLocation){
NSLog(@"Location is: %@ ",elementName);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([elementName isEqualToString:@"Location"] && isLocation){
NSLog(@"Location is: %@ ",string);
[locationArray addObject:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Location"]) {
isLocation=NO;
}
}
注意:最后_locationArray将拥有所有位置的id。