如何阅读XML标记的内容

时间:2022-10-27 10:36:33

How to read the content of this XML?

如何阅读此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:

我可以解析它,它可以工作,但我无法访问Location(4)的值。我在用着:

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

1 个解决方案

#1


0  

I have already posted the answer please follow the following link 'https://*.com/questions/10877374/how-to-parse-the-attributes-like-in-this-xml-file-through-xml-parser/10877758#10877758'

我已经发布了答案,请点击以下链接'https://*.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.

首先拿一个bool变量。然后编写以下代码。也许它会帮助你我几天前完成了相关的工作。

First .h file

第一个.h文件

@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
    NSMutableArray *_locationArray;
    BOOL isLocation;
}

Then in .m file you should write this.

然后在.m文件中你应该写这个。

//In ViewDidLoad

//在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

//现在在xml Parser Delegate Methods中使用pragma mark - 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;
    }
}

Note: At the end _locationArray will be having all the location's id's.

注意:最后_locationArray将拥有所有位置的id。

#1


0  

I have already posted the answer please follow the following link 'https://*.com/questions/10877374/how-to-parse-the-attributes-like-in-this-xml-file-through-xml-parser/10877758#10877758'

我已经发布了答案,请点击以下链接'https://*.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.

首先拿一个bool变量。然后编写以下代码。也许它会帮助你我几天前完成了相关的工作。

First .h file

第一个.h文件

@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
    NSMutableArray *_locationArray;
    BOOL isLocation;
}

Then in .m file you should write this.

然后在.m文件中你应该写这个。

//In ViewDidLoad

//在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

//现在在xml Parser Delegate Methods中使用pragma mark - 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;
    }
}

Note: At the end _locationArray will be having all the location's id's.

注意:最后_locationArray将拥有所有位置的id。