NSXMLParser Separate Three Elements
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!");}
}
精彩评论