How to read the content of an XML tag
How to read the content of this XML?
<Locations>
<Location>4</Location>
</Locations>
I can parse it, and it works, but I can't access the value of Location
(4
). I'm using:
ZoneCheck 开发者_Go百科*azone;
NSString *url1 = [NSString stringWithFormat:@"%@", azone.location];
I have already posted the answer please follow the following link 'https://stackoverflow.com/questions/10877374/how-to-parse-the-attributes-like-in-this-xml-file-through-xml-parser/10877758#10877758'
First take a bool variable. and then write the following code. perhaps it will help you I have done the related work , a few days ago.
First .h file
@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
NSMutableArray *_locationArray;
BOOL isLocation;
}
Then in .m file you should write this.
//In 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];
}
// Now In xml Parser Delegate Methods pragma mark - NSXMLParser Delegate Method
- (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;
}
}
Note: At the end _locationArray will be having all the location's id's.
精彩评论