2 ooomiao oOOMIAO 于 2013.05.29 10:41 提问

XML回应到NSArray的问题

解析XML文件获取tagid" 和"mac两个节点的内容。应该如何将内容存储到两个数组中?

一个是tagid一个是mac。

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    self.currentElement = elementName;
    self.currentElement2 = elementName;

    if ([self.currentElement isEqualToString:@"mac"]) {
        self.currentTitle = [NSMutableString string];

    }

    if ([self.currentElement2 isEqualToString:@"tagid"]) {
        self.currentTitle2 = [NSMutableString string];

    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([self.currentElement isEqualToString:@"mac"]) {
        NSLog(@"%@", self.currentTitle);
    }

    if ([self.currentElement2 isEqualToString:@"tagid"]) {
        NSLog(@"%@", self.currentTitle2);
    }

    self.currentTitle = nil;
    self.currentElement = nil;
    self.currentTitle2 = nil;
    self.currentElement2 = nil;

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if (!self.currentElement) return;
    if ([self.currentElement isEqualToString:@"mac"]) {
        self.currentTitle = string;

    }

    if (!self.currentElement2) return;
    if ([self.currentElement2 isEqualToString:@"tagid"]) {
        self.currentTitle2 = string;
    }

}
- (IBAction)aktualisieren:(id)sender {

    NSURL *xmlURL = [[NSURL alloc] initWithString:@"http://mysite/mycontent"];

    parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    [parser setDelegate:self];
    [parser parse];
}

2个回答

abcdeFGh_xyz
abcdeFGh_xyz   2013.05.29 15:48

parseStartDocument 中创建两个数组,一个给mac-MacArray,另一个给tagid-tagArray。

didEndElement方法中,检查元素,添加你的self.currentTitle到数组中。

Chengzi_963
Chengzi_963   2013.05.29 15:54

需要两个数组:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

self.currentElement = elementName;
self.currentElement2 = elementName;

if ([self.currentElement isEqualToString:@"mac"]) {
    self.currentTitle = [NSMutableString string];
    NSMutableArray *macArray = [NSMutableArray alloc] init];
}

if ([self.currentElement2 isEqualToString:@"tagid"]) {
    self.currentTitle2 = [NSMutableString string];
    NSMutableArray *tagIdArray = [NSMutableArray alloc] init];
}
}


-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([self.currentElement isEqualToString:@"mac"]) {
    NSLog(@"%@", self.currentTitle);
    [macArray addObject:self.currentTitle];
}

if ([self.currentElement2 isEqualToString:@"tagid"]) {
    NSLog(@"%@", self.currentTitle2);
    [tagIdArray addObject:self.currentTitle2];
}

self.currentTitle = nil;
self.currentElement = nil;
self.currentTitle2 = nil;
self.currentElement2 = nil;

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!