iPhone-解析XML文件-请帮助!
我需要解析xml文件的帮助。我的问题是我不知道如何实现DiEndElement委托。iPhone-解析XML文件-请帮助!,iphone,xml,uitableview,nsxmlparser,Iphone,Xml,Uitableview,Nsxmlparser,我需要解析xml文件的帮助。我的问题是我不知道如何实现DiEndElement委托。 我想要的是,我将有两个单元,在那里展示旧约和新约,然后是圣经的书籍和章节。 如果我能在xml解析方面得到一些帮助,剩下的我就可以处理了 我将非常感谢您的帮助 谢谢和问候 我的xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <bible> <testament name="Old Testament"> <book
我想要的是,我将有两个单元,在那里展示旧约和新约,然后是圣经的书籍和章节。 如果我能在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>
以下是调试器控制台的输出:
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
你已经在分析它了。在
didEndElement:
调用中,只需对元素执行任何操作。因为您的XML不包含任何包装字符串(您没有使用foundCharacters:
),所以您所要做的就是相应地响应didStartElement:
和didEndElement:
。如果需要捕获属性或分配新的数据结构来容纳可能的子级,请在didStartElement:
中执行。如果需要将对象保存到集合中或以某种方式完成特定元素的处理,请在didEndElement:
中执行
这个问题实际上不是关于解析的,而是关于您想要为响应解析而执行的任何逻辑
编辑以下评论:
在解析过程中,我通常会执行以下操作来保存对象:在我的接口中,我声明了需要将对象保存到的集合,以及一个临时对象,在将其添加到集合之前,我使用该临时对象保存所需的任何数据,如下所示
@interface MyClass : NSObject <NSXMLParserDelegate>{
NSMutableArray *collection_;
SomeObject *tempObject_;
}
@end
然后对集合对象执行您将要执行的操作。确保你处理了内存问题,比如释放集合对象或者其他什么。我通常使用委托回调(我自己设计的)之类的方法将集合获取到模型,以便从逻辑上将解析与模型分离。不要再次回滚我的编辑欢迎使用堆栈溢出。这个问题完全无法理解。请使用“101010”按钮格式化您的代码,并在点击post之前检查预览。如果你看不懂,那我们怎么看呢?很抱歉。我在想怎么做。谢谢你,理查德!嗨Kevboh,我感谢你的帮助。事实上,我正在尝试“将对象保存到集合中”。这正是我遇到问题的地方。任何帮助都将不胜感激。Kevboh,非常感谢!这对我帮助很大!我可以从这里开始。嗨,凯夫布,非常感谢!很高兴知道像你这样的人会花时间伸出援助之手!
//
// 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
@interface MyClass : NSObject <NSXMLParserDelegate>{
NSMutableArray *collection_;
SomeObject *tempObject_;
}
@end
- (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;
}