我已經貼出答案請按照以下鏈接'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的。
你使用什麼語言? C#,JAVA,C + +,JavaScript的? – griegs 2010-07-01 04:50:05
我打算猜測Objective-C,它基於懸而未決的代碼示例的開頭。 – 2010-07-01 04:59:00
是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