Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML response to NSArray

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 :)

like image 238
user2428412 Avatar asked Dec 27 '22 03:12

user2428412


2 Answers

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.

like image 62
Ravindra Bagale Avatar answered Dec 29 '22 11:12

Ravindra Bagale


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;

}
like image 34
Durgaprasad Avatar answered Dec 29 '22 12:12

Durgaprasad