2012-03-31 23 views
1

NSXMLParserをセットアップする方法を見つけるためにここ数日間検索してきました。私が解析する必要があるXMLファイルは、以下の通りである:私はNSXMLParserのデリゲートメソッド実装したNSXMLParserを設定してローカルのXMLファイルを解析する

<matrix> 
    <row>eraser*met</row> 
    <row>debone*anat</row> 
    <row>ani*jalisco</row> 
    <row>madwoman*on</row> 
    <row>**joy*itsme</row> 
    <row>isao***amad</row> 
    <row>mends*mio**</row> 
    <row>be*parental</row> 
    <row>insipid*hai</row> 
    <row>bail*modern</row> 
    <row>esse*scored</row> 
</matrix> 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

をしかし、私は実際には「マトリックス」をステップ実行する方法を見つけ出すことはできません行を配列に保存します。

私があなたを助けてくれることを願っています。

敬具 セバスチャン

編集:ここでは

が全体のXMLファイルです:

<?xml version="1.0" encoding="UTF-8"?> 
<grid version="1"> 
    <matrix> 
     <row>eraser*met*</row> 
     <row>debone*anat</row> 
     <row>ani*jalisco</row> 
     <row>madwoman*on</row> 
     <row>**joy*itsme</row> 
     <row>isao***amad</row> 
     <row>mends*mio**</row> 
     <row>be*parental</row> 
     <row>insipid*hai</row> 
     <row>bail*modern</row> 
     <row>esse*scored</row> 
    </matrix> 
    <clues> 
     <across> 
      <clue>Rubber</clue> 
      <clue>Intro to physics?</clue> 
      <clue>Fish prep?</clue> 
      <clue>Med school subj.</clue> 
      <clue>Tropical cuckoo bird</clue> 
      <clue>State in W Mexico</clue> 
      <clue>Insane female</clue> 
      <clue>Not off</clue> 
      <clue>Happiness</clue> 
      <clue>&quot;Who's there?&quot; response</clue> 
      <clue>Golfer Aoki</clue> 
      <clue>Diary of ___ Housewife</clue> 
      <clue>Fixes</clue> 
      <clue>O Sole ___</clue> 
      <clue>To exist</clue> 
      <clue>Maternal or paternal</clue> 
      <clue>Vapid</clue> 
      <clue>Yes, in Yokohama</clue> 
      <clue>Remove water from a boat</clue> 
      <clue>Contemporary</clue> 
      <clue>&quot;___ quam videri&quot; (North Carolina's motto)</clue> 
      <clue>Tallied</clue> 
     </across> 
     <down> 
      <clue>Dutch cheese</clue> 
      <clue>Drink</clue> 
      <clue>Actress Sofer</clue> 
      <clue>Perceived to be</clue> 
      <clue>Ivory Coast's largest city</clue> 
      <clue>Lisa, to Bart, briefly</clue> 
      <clue>Therefore</clue> 
      <clue>Stack of firewood</clue> 
      <clue>Take pleasure in</clue> 
      <clue>Drain</clue> 
      <clue>500 sheets</clue> 
      <clue>Lens holders</clue> 
      <clue>My ___, Vietnam</clue> 
      <clue>Red Bordeaux</clue> 
      <clue>Preserve</clue> 
      <clue>Perform</clue> 
      <clue>Printing widths</clue> 
      <clue>Suffocate</clue> 
      <clue>Puget Sound city</clue> 
      <clue>Swiss river</clue> 
      <clue>Did penance</clue> 
      <clue>Swedish soprano Jenny</clue> 
     </down> 
    </clues> 
    <hints> 
     <across> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
     </across> 
     <down> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
     </down> 
    </hints> 
</grid> 

私はまだヒントで入力していないが、彼らはそこになりますリリース前。

+0

あなたはこのデータのみを持っています。より多くのデータがxmlファイルに入っています。plsは私にXMLの全フォーマットを与えます。それは解析コードを与えるでしょう。それはnsxmlパーサーより優れています。 – parag

+0

こんにちは。迅速な対応をありがとう。あなたはXMLファイル全体を見ることができるように私の投稿を編集しました –

+0

完全なXMLを解析するコードを作れますか? :) –

答えて

1

あなたはすでに正しい軌道に乗っています。 NSXMLParserは、parseメソッドを呼び出すと、これらのメソッドの呼び出しを開始します。あなたの質問はいくつかのサンプルxmlを持っているので、カスタムNSXMLParserDelegateクラスを実装する方法の(ラフ)サンプルがあります。注意してください私のプロジェクトフォルダ内の "MatrixList.xml"という名前のファイルに上記のXMLをコピーしました。

MatrixList.h

#import <Foundation/Foundation.h> 
@interface MatrixList : NSObject <NSXMLParserDelegate> 
@property (readonly) NSMutableArray *rows; // property to access results 
-(id)initWithContentsOfURL:(NSURL *)url; 
@end 

MatrixList.m

#import "MatrixList.h" 
@implementation MatrixList{ 
    NSXMLParser *parser; 
    NSMutableString *charactersFound; 
} 
@synthesize rows = _rows; 
-(void)parserDidStartDocument:(NSXMLParser *)parser{ 
     // These objects are created here so that if a document is not found they will not be created 
    _rows = [[NSMutableArray alloc] init]; 
    charactersFound = [[NSMutableString alloc] init]; 
} 
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    // clear the characters for new element 
    [charactersFound setString:@""]; 
} 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    // add string found to the mutable string 
    [charactersFound appendString:string]; 
} 
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
    if ([elementName isEqualToString:@"row"]){ 
     // If we are done with a row add the rows contents, a string, to the rows array 
     [_rows addObject:[charactersFound copy]]; 
    } 
    [charactersFound setString:@""]; 
} 
-(void)parserDidEndDocument:(NSXMLParser *)parser{ 
    // This method is handy sometimes 
} 
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{ 
    NSLog(@"error:%@",parseError.localizedDescription); 
} 
-(id)initWithContentsOfURL:(NSURL *)url{ 
    if ((self = [super init])){ 
     parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
     parser.delegate = self; 
     [parser parse]; // This is for an example, You might not want to call parse here, depending on context 
    } 
    return self; 
} 
@end 

このクラスはそうように使用されている:上記のコードで

// My copy is in the bundle, You could use a url for the docs directory instead 
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"MatrixList" withExtension:@"xml"]; 
MatrixList *matrix = [[MatrixList alloc] initWithContentsOfURL:fileURL]; 
NSLog(@"rows:%@",matrix.rows); 

コンソールが生成される:

rows:(
    "eraser*met", 
    "debone*anat", 
    "ani*jalisco", 
    "madwoman*on", 
    "**joy*itsme", 
    "isao***amad", 
    "mends*mio**", 
    "be*parental", 
    "insipid*hai", 
    "bail*modern", 
    "esse*scored" 
) 

このコードは洗練されていませんが、基本的なXMLを解析する方法の良い例です。

+0

ありがとうたくさんのNJones :)あなたは最高と最速の方法が配列内のすべての単一の文字を格納することを知っていますか? :) –

関連する問題