2010-12-08 11 views
0

xmlファイルの解析に助けが必要です。私の問題は、didEndElementデリゲートの実装方法がわかりません。
私が望むのは、旧約聖書と新約聖書が表示され、次に聖書と章が表示される2つのセルがあるということです。 私はちょうど私が管理することができる残りの部分を解析するXMLでいくつかの助けを得ることができます。iPhone - 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

ようこそスタックオーバーフロー。この質問は完全にそして全く読めない。 "101010"ボタンを使用してコードをフォーマットし、投稿をヒットする前にプレビューを確認してください。あなたがそれを読むことができないなら、どうすればそれを読むことができますか? –

+0

私は彼のためにそれを修正しました –

答えて

0

あなたはすでにそれを解析しています。 didEndElement:コールでは、要素で何をしたいかを実行します。 XMLにはラップされた文字列(foundCharacters:を使用していない)が含まれていないので、didStartElement:didEndElement:に応じて対応するだけです。子を保持するために属性を取得するか、新しいデータ構造を割り当てる必要がある場合は、didStartElement:で行います。オブジェクトをコレクションに保存したり、特定の要素の処理を何らかの方法で終了したりする必要がある場合は、didEndElement:で行います。

この質問は、実際に解析することではなく、解析に応じてどの論理を適用するかについてです。

編集下記のコメントへの返信

私は通常、解析中にオブジェクトをオフに保存するには次のようにします。私のインタフェースで、私はにオブジェクトを保存する必要が収集し、私が使用する一時オブジェクトを宣言私がする必要がどのようなデータを保持するための実装では、この

のように、コレクションに追加する前に、私は一般的にdidStartDocumentに、これら2つのオブジェクトを操作する:、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; 
} 

次に、コレクションオブジェクトで行うことをしてください。コレクションオブジェクトなどを解放するなど、メモリの処理を確実に行ってください。私は通常モデルからの構文解析を論理的に分離するために、モデルにコレクションを取得するためにデリゲートコールバック(私自身の工夫)のようなものを使用します。

+0

こんにちはKevboh、私はあなたの助けに感謝します。私は実際にオブジェクトをコレクションに保存しようとしています。それは私が問題にぶつかるところです。どんな助けもありがとう。 – tssav

+0

私の編集を参照してください、いくつかの解析コードが含まれています。 – kevboh

+0

Kevboh、ありがとう!これは私にとって大きな助けになります!私はここから管理することができます。 – tssav

関連する問題