我从我的服务器返回了这个相当复杂的 XML,我需要一些帮助来将其解析为我可以访问以供以后使用的对象。
这就是xml的样子
<Eng>
<Result >
<Series >
<Link/>
<FMF/>
<AlsoLink/>
<Plugins/>
</FMF>
<Sheet >
<Spaces>
<Spacing >
<Names/>
</Spacing>
<Spacing >
<Names/>
</Spacing>
</Spaces>
</Sheet>
</Series>
</Result>
</Eng>
然后,我使用 NSXMLParser 尝试将 xml 中每个元素的所有“ObjectForKey/s”解析到他们自己的字典中。如果这有意义的话。
这就是我的代码目前的样子
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Series"]){
parsedMutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:attributeDict];
self.parsedDataArrayOfDictionaries = [NSMutableArray arrayWithCapacity:8];
if ([elementName isEqualToString:@"Link"]) {
}
if ([elementName isEqualToString:@"FMF"]) {
if ([elementName isEqualToString:@"AlsoLink"]) {
}
else if ([elementName isEqualToString:@"Plugins"]) {
}
}
else if ([elementName isEqualToString:@"Sheet"]) {
if ([elementName isEqualToString:@"Spaces"]) {
if ([elementName isEqualToString:@"Spacing"]) {
if ([elementName isEqualToString:@"Names"]) {
}
}
}
}
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"Series"]){
[parsedDataArrayOfDictionaries addObject:parsedMutableDictionary];
if ([elementName isEqualToString:@"Link"]) {
}
if ([elementName isEqualToString:@"FMF"]) {
if ([elementName isEqualToString:@"AlsoLink"]) {
}
else if ([elementName isEqualToString:@"Plugins"]) {
}
}
else if ([elementName isEqualToString:@"Sheet"]) {
if ([elementName isEqualToString:@"Spaces"]) {
if ([elementName isEqualToString:@"Spacing"]) {
if ([elementName isEqualToString:@"Names"]) {
}
}
}
}
}
// parsedMutableDictionary = nil;
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Paser Error = %@", parseError);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A parsing failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
//TODO: Look into how to use this method over multiple views. i.e.(other requests such as keycode, advanced )
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
// display values
NSArray *filteredArray = parsedDataArrayOfDictionaries;
NSLog(@"%@", filteredArray);
}
我遇到的问题是我不明白我应该在他们自己的对象中拥有什么......而且当其中一些值返回时,我可能有多个 Spacing 元素,我只是不知道如何处理......
任何帮助或提示将不胜感激,如果我没有指定一些东西只是问我的大脑正在努力解决这个问题。
谢谢。