4

I parse a xml file to get the content of two nodes "tagid" and "mac". How can I store this content in two arrays, one for "tagid" and one for "mac"?

The "parser" works. Thank you in advance

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

Thank you for your responses :)

2

5 Answers 5

5

create two arrays in parseStartDocument. one for mac-MacArray , one for tagid-tagArray.

in didEndElement method,check for element & add your self.currentTitle to that perticular your Array.

3

In .h declare two arrays tagArray and macArray. In viewDidLoad alloc init both arrays.

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

}
2

You need two different arrays

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

}
0
0

Declare two mutable arrays:

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
}

Find detailed parsing example here

0
0

By the way, if either of these was a long string, might get separated into multiple calls to foundCharacters. Thus, foundCharacters should always append strings, not setting strings:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.