全,
我有以下格式的XML:
<linked-list>
<Description>
<desc></desc>
<IP></IP>
</Description>
</linked-list>语句的内部可以有无限多个。
我应该如何使用NSXMLParser来解析它?我当前的代码如下所示,但它不能正确解析。
@implementation XMLParser
@synthesize response;
- (XMLParser *) initXMLParser
{
self = [super init];
// init dictionary of response data
response = [[NSMutableDictionary alloc] init];
return self;
}
//Gets Start Element of SessionData
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"linked-list"])
{
NSLog(@"Found linked-list in the return XML! Continuing...");
//response is a NSMutableArray instance variable
//THIS SHOULD NEVER NEED TO BE USED
if (!response)//if array is empty, it makes it!
{
NSLog(@"Dictionary is empty for some reason, creating...");
response = [[NSMutableDictionary alloc] init];
}
//END: THIS SHOULD NEVER BE USED
return;
}
else
{
currentElementName = elementName;
NSLog(@"Current Element Name = %@", currentElementName);
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
[currentElementValue setString:string];
NSLog(@"Processing value for : %@", string);
}
}
//Gets End Element of linked-list
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"linked-list"])
{
// We reached the end of the XML document
// dumps dictionary into log
NSLog(@"Dump:%@", [response description]);
return;
}
else
{
//Adds key and object to dictionary
[response setObject:currentElementValue forKey:currentElementName];
NSLog(@"Set values, going around again... brb.");
}
currentElementValue = nil;
currentElementName = nil;
}
@end发布于 2011-07-16 03:35:27
一些观察结果:
Description元素,则存储内容的外部数据结构必须是NSMutableArray而不是字典。然后,您在didStartElement:中为每个Description element.@"Description",如果是,则创建一个新的NSMutableDictionary实例,并将其存储在一个ivar.foundCharacters:中,您总是必须将新字符附加到现有的currentElementValue中,因为该方法可以为每个元素的内容调用多次。我看到许多人这样做是错误的,尽管苹果的示例代码清楚地展示了正确的方法。didEndElement:,这样做:- If the element name is `@"desc"` or `@"IP"`, assign `currentElementValue` to the corresponding key in your current mutable dictionary. Don't forget to release `currentElementValue` before you set it to `nil`. You currently have a memory leak in your code because you're not doing that.
- If the element name is `@"Description"`, add the current mutable dictionary to the mutable array. Release the dictionary and set the ivar to `nil`. A new dictionary will be created the next time you encounter a `@"Description"` element in `didStartElement:`.
- If the element name is `@"linked-list"`, the mutable array will contain all the dictionaries and you're done.
https://stackoverflow.com/questions/6712150
复制相似问题