我需要帮助解析xml文件。我的问题是我不知道如何实现didEndElement委托 我想要的是,我将有2个单元格,其中将显示旧约和新约,然后是圣经书籍和章节。 如果我可以获得一些xml解析的帮助,我可以管理其余的。
非常感谢任何帮助!
谢谢和问候!
我的xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<bible>
<testament name="Old Testament">
<book name="Genesis">
<chapter id="Genesis 1"></chapter>
<chapter id="Genesis 2"></chapter>
</book>
<book name="Exodus">
<chapter id="Exodus 1"></chapter>
<chapter id="Exodus 2"></chapter>
</book>
</testament>
<testament name="New Testament">
<book name="Matthew">
<chapter id="Matthew 1"></chapter>
<chapter id="Matthew 2"></chapter>
</book>
<book name="Revelation">
<chapter id="Revelation 1"></chapter>
<chapter id="Revelation 2"></chapter>
</book>
</testament>
</bible>
// Bible.h
#import <Foundation/Foundation.h>
@interface Bible : NSObject {
NSMutableArray *bible;
NSMutableArray *testament;
NSMutableArray *book;
NSString *chapterID;
}
@property (nonatomic, retain)NSMutableArray *bible;
@property (nonatomic, retain)NSMutableArray *testament;
@property (nonatomic, retain)NSMutableArray *book;
@property (nonatomic, retain)NSString *chapterID;
@end
// Bible.m
#import "Bible.h"
@implementation Bible
@synthesize bible;
@synthesize testament;
@synthesize book;
@synthesize chapterID;
- (void) dealloc {
[bible release];
[testament release];
[book release];
[chapterID release];
[super dealloc];
}
@end
//
// XMLParser.h
// BibleXML
//
#import <Foundation/Foundation.h>
#import "Bible.h"
@protocol NSXMLParserDelegate;
@class BibleXMLAppDelegate, Bible;
@interface XMLParser : NSObject <NSXMLParserDelegate> {
NSMutableString *currentElementValue;
BibleXMLAppDelegate *appDelegate;
Bible *theBible;
}
- (XMLParser *) initXMLParser;
@end
//
// XMLParser.m
#import "XMLParser.h"
#import "BibleXMLAppDelegate.h"
#import "Bible.h"
@implementation XMLParser
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (BibleXMLAppDelegate *) [[UIApplication sharedApplication] delegate];
return self;
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"bible"]) {
NSLog(@"Found element: %@", elementName);
appDelegate.bible = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"testament"]) {
theBible = [[Bible alloc] init];
//Extract the attribute here.
theBible.testament = [attributeDict valueForKey:@"name"];
NSLog(@"Testament: %@", theBible.testament);
return;
}
else if ([elementName isEqualToString:@"book"])
{
theBible.book = [attributeDict valueForKey:@"name"];
NSLog(@"Book: %@", theBible.book);
return;
}
else if([elementName isEqualToString:@"chapter"])
{
theBible.chapterID =[attributeDict objectForKey:@"id"];
NSLog(@"Chapter: %@", theBible.chapterID);
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"bible"]){
return;
}
}
- (void) dealloc {
[theBible release];
[currentElementValue release];
[super dealloc];
}
@end
以下是调试器控制台的输出:
2010-12-08 19:53:10.101 BibleXML[25641:207] found file and started parsing
2010-12-08 19:53:10.102 BibleXML[25641:207] Found element: bible
2010-12-08 19:53:10.103 BibleXML[25641:207] Testament: Old Testament
2010-12-08 19:53:10.103 BibleXML[25641:207] Book: Genesis
2010-12-08 19:53:10.104 BibleXML[25641:207] Chapter: Genesis 1
2010-12-08 19:53:10.104 BibleXML[25641:207] Chapter: Genesis 2
2010-12-08 19:53:10.105 BibleXML[25641:207] Book: Exodus
2010-12-08 19:53:10.105 BibleXML[25641:207] Chapter: Exodus 1
2010-12-08 19:53:10.106 BibleXML[25641:207] Chapter: Exodus 2
2010-12-08 19:53:10.107 BibleXML[25641:207] Testament: New Testament
2010-12-08 19:53:10.107 BibleXML[25641:207] Book: Matthew
2010-12-08 19:53:10.108 BibleXML[25641:207] Chapter: Matthew 1
2010-12-08 19:53:10.108 BibleXML[25641:207] Chapter: Matthew 2
2010-12-08 19:53:10.109 BibleXML[25641:207] Book: Revelation
2010-12-08 19:53:10.109 BibleXML[25641:207] Chapter: Revelation 1
2010-12-08 19:53:10.110 BibleXML[25641:207] Chapter: Revelation 2
2010-12-08 19:53:10.110 BibleXML[25641:207] No Errors
答案 0 :(得分:0)
你已经在解析它了。在didEndElement:
调用中,只需对元素执行任何操作即可。由于您的XML不包含任何包装的字符串(您没有使用foundCharacters:
),因此您只需相应地回复didStartElement:
和didEndElement:
。如果您需要捕获属性或分配新数据结构来容纳可能的子项,请在didStartElement:
中执行此操作。如果您需要将对象保存到集合中或以某种方式完成特定元素的处理,请在didEndElement:
中执行此操作。
这个问题并不是解析,而是关于你想要解析的任何逻辑。
根据以下评论进行修改:
我通常在解析过程中执行以下操作来保存对象:在我的界面中,我声明了我需要将对象保存到的集合以及一个临时对象,用于在将其添加到集合之前保存我需要的任何数据,像这样
@interface MyClass : NSObject <NSXMLParserDelegate>{
NSMutableArray *collection_;
SomeObject *tempObject_;
}
@end
在实现中,我操纵这两个对象,通常在didStartDocument
:,didStartElement:
和didEndElement:
中,如下所示:
- (void)parserDidStartDocument:(NSXMLParser *)parser {
collection_ = [[NSMutableArray alloc] init];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
//if your object is tied to a tag that wraps text (delivered in foundCharacters:), initialize it here
tempObject_ = [[SomeObject alloc] init];
//maybe you need the attributes....
tempObject_.someProperty = [attributes objectForKey:@"attribute-name"];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
//when the tag ends, you can save it off into the collection
[collection_ addObject:tempObject_];
[tempObject_ release];
tempObject_ = nil;
}
然后用集合对象做你想做的事。确保处理内存事务,例如释放集合对象或其他内容。我通常使用像委托回调(我自己设计的)来获取模型的集合,以便在逻辑上将解析与模型分开。