4

xml ファイルを解析して、2 つのノード "tagid" と "mac" のコンテンツを取得します。このコンテンツを「tagid」用と「mac」用の 2 つの配列に保存するにはどうすればよいですか?

「パーサー」が機能します。前もって感謝します

 -(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];
}

回答ありがとうございます:)

4

5 に答える 5

3

.h で、tagArray と macArray の 2 つの配列を宣言します。viewDidLoad では、両方の配列を alloc init します。

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

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

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

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

}
于 2013-05-28T12:12:19.863 に答える
2

2 つの異なる配列が必要です

-(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;

}
于 2013-05-28T12:15:12.767 に答える
0

ところで、これらのいずれかが長い文字列の場合、複数の への呼び出しに分割される可能性がありますfoundCharacters。したがって、foundCharacters文字列を設定するのではなく、常に文字列を追加する必要があります。

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

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

    if (!self.currentElement2) return;
    if ([self.currentElement2 isEqualToString:@"tagid"]) {
        [self.currentTitle2 appendString:string];
    }
}
于 2013-05-28T13:01:16.867 に答える
0

2 つの可変配列を宣言します。

NSMutableArray *macArray , *tagidArray;

macArray = [[NSMutableArray alloc]init];
tagidArray = [[NSMutableArray alloc]init];

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

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

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

}

-(void)parserDidEndDocument{

//Do something as parsing has been completed here and your values are in two arrays
}

詳細な解析例はこちら

于 2013-05-28T12:19:30.407 に答える