gpt4 book ai didi

iphone - 设置 NSXMLParser 来解析本地 XML 文件

转载 作者:行者123 更新时间:2023-11-29 13:38:10 24 4
gpt4 key购买 nike

最近几天我一直在寻找一种方法来设置 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 委托(delegate)方法:

- (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>

我还没有输入提示,但它们会在发布前出现。

最佳答案

您已经在正确的轨道上了。 NSXMLParser 将在您调用 parse 方法时开始调用这些方法。因为您的问题有一些示例 xml,所以这里是一个(ruff)示例,说明如何实现自定义 NSXMLParserDelegate 类。请注意,我将上述 xml 复制到项目文件夹中名为“MatrixList.xml”的文件中。

矩阵列表.h:

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

矩阵列表.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。

关于iphone - 设置 NSXMLParser 来解析本地 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com