开发者

NSXMLParser Separate Three Elements

开发者 https://www.devze.com 2023-02-01 18:47 出处:网络
I am parsing an xml file with three of the same elements: \"im:imageLink\". Currently I am adding all three links to an array:

I am parsing an xml file with three of the same elements: "im:imageLink". Currently I am adding all three links to an array:

if ([currentElement isEqualToString:@"im:image"]) {   
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAn开发者_开发百科dNewlineCharacterSet]];
    [imageLinks addObject:string];
} 

I would really appreciate it if you could tell me how I could separate each of the three elements, and put them in three different arrays.


If I understand the comments above, you want to separate the items by the height attribute, which seems to come in 3 known values: 55, 60, and 170. In this case, you could split them based on the attribute dictionary passed in to the parser:didStartElement:... call. Presuming you called the attributes variable "attributeDict", your call would look something like:

int height = [[attributeDict valueForKey:@"height"] intValue];

From there you can use a typical if...then...else construction to stuff the results into the three different arrays. Like:

if ([currentElement isEqualToString:@"im:image"]) {   
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    int height = [[attributeDict valueForKey:@"height"] intValue];
    if (55 == height) {[imageLinks55 addObject:string];}
    else if (60 == height) {[imageLinks60 addObject:string];}
    else if (170 == height) {[imageLinks170 addObject:string];}
    else {NSLog(@"Unrecognized height of an image!");}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消