Iphone 设置NSXMLParser以解析本地XML文件

Iphone 设置NSXMLParser以解析本地XML文件,iphone,ios,xml,xml-parsing,nsxmlparser,Iphone,Ios,Xml,Xml Parsing,Nsxmlparser,在过去几天中,我一直在搜索如何设置NSXMLParser。我需要解析的XML文件如下所示: <matrix> <row>eraser*met</row> <row>debone*anat</row> <row>ani*jalisco</row> <row>madwoman*on</row> <row>**joy*itsme</row

在过去几天中,我一直在搜索如何设置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>
但我不知道如何实际逐步通过“矩阵”并将行保存到数组中

我希望你能帮助我

致意 塞巴斯蒂安

编辑:

以下是整个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>

橡皮擦*
德伯恩*阿纳特
阿尼·贾利斯科
疯女人
**乔伊,是我
伊索·阿马德
弥**
做父母
平淡的*海
保释金*现代
埃斯*得分
橡皮
物理入门?
准备鱼?
医学院科目。
热带杜鹃鸟
西墨西哥州
疯女人
不关
幸福
“谁在那儿?”回答
高尔夫球手青木
家庭主妇日记
修复
奥索尔___
存在
母亲的还是父亲的
乏味的
是的,在横滨
从船上取水
当代的
“维德利”(北卡罗来纳州的座右铭)
计算
荷兰干酪
喝
女演员索弗
被认为是
象牙海岸最大城市
丽莎,给巴特,简短地
所以
一堆柴火
乐在其中
排水
500张
镜头支架
我的,越南
波尔多红葡萄酒
保存
表演
印刷宽度
窒息
普吉特桑德城
瑞士河
忏悔
瑞典女高音珍妮

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

您已经走上了正确的轨道。当您在其上调用
parse
方法时,
NSXMLParser
将开始调用这些方法。因为您的问题有一些示例xml,所以这里有一个(ruff)示例,介绍如何实现自定义
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
此类的使用方式如下:

// 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的通用示例。

您已经走上了正确的道路。当您在其上调用
parse
方法时,
NSXMLParser
将开始调用这些方法。因为您的问题有一些示例xml,所以这里有一个(ruff)示例,介绍如何实现自定义
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
此类的使用方式如下:

// 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的通用示例。

您只有这些数据,或者更多数据来自xml文件,请给我xml的完整格式,我将给出一段解析代码。这比nsxml parser.Hi更好。谢谢你的快速回复。我编辑了我的帖子,以便您可以看到整个XML文件。您可以编写代码来解析完整的XML吗?:)你有这个数据只有或更多的数据在xml文件中,请给我xml的整个格式,我会给一个解析代码,它比nsxml parser.Hi更好。谢谢你的快速回复。我编辑了我的帖子,以便您可以看到整个XML文件。您可以编写代码来解析完整的XML吗?:)非常感谢NJones:)你知道在数组中存储每个字符的最好和最快的方法是什么吗非常感谢NJones:)你知道在数组中存储每个字符的最好和最快的方法是什么吗