2010-07-01 64 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的。