安装NSXMLParser来解析本地XML文件

问题描述:

我一直在搜索最近几天找到一种方法来设置NSXMLParser。该XML文件我需要解析如下:安装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> 

我已经实现了的NSXMLParser委托方法:

- (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文件请给我的XML整个格式我会给一个解析代码,它是更好的,然后nsxml分析器。 – parag 2012-03-31 18:00:58

+0

嗨。感谢你的快速回复。我编辑了我的文章,以便您可以看到整个XML文件 – 2012-04-04 05:56:06

+0

您可以让代码解析完整的XML文件吗? :) – 2012-04-05 15:26:42

您目前已走上正轨。当您调用parse方法时,NSXMLParser将开始调用这些方法。因为你的问题有一些示例xml,这里是如何实现定制NSXMLParserDelegate类的一个(流行)示例。请注意我将上面的xml复制到我的项目文件夹中名为“MatrixList.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 :)你知道什么是最好的和最快的方式来存储阵列中的每个单个字符? :) – 2012-04-10 20:32:47