2010-07-01 68 views
-2

如何读取此XML的内容?如何读取XML标记的内容

<Locations> 
<Location>4</Location> 
</Locations> 

我可以分析它,和它的作品,但我不能访问的Location4)的值。我使用的是:

ZoneCheck *azone; 
NSString *url1 = [NSString stringWithFormat:@"%@", azone.location]; 
+1

你使用什么语言? C#,JAVA,C + +,JavaScript的? – griegs 2010-07-01 04:50:05

+2

我打算猜测Objective-C,它基于悬而未决的代码示例的开头。 – 2010-07-01 04:59:00

+0

是iphone 我得到它,如果([的ElementName isEqualToString:@ “位置”]){ \t \t \t \t [appDelegate.zone ADDOBJECT:currentElementValue]。 \t \t \t \t [aZonecheck setValue:currentElementValue forKey:elementName]; 谢谢 – ram 2010-07-01 06:29:06

回答

0

我已经贴出答案请按照以下链接'https://stackoverflow.com/questions/10877374/how-to-parse-the-attributes-like-in-this-xml-file-through-xml-parser/10877758#10877758'

先来布尔变量。然后编写下面的代码。也许它会帮助你,我前几天做了相关的工作。

首先.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解析器委托方法 编译标志 - 的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的。