4

我解析一个 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];
}

谢谢您的反馈 :)

4

5 回答 5

5

中创建两个数组parseStartDocument。一种用于 mac-MacArray ,一种用于 tagid-tagArray。

didEndElement方法中,检查元素并将您添加self.currentTitle到特定的数组中。

于 2013-05-28T12:10:17.033 回答
3

在 .h 中声明两个数组 tagArray 和 macArray。在 viewDidLoad alloc 中初始化两个数组。

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

你需要两个不同的数组

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

声明两个可变数组:

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 回答
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 回答