Objective-C中的复杂XML解析

时间:2012-07-02 13:43:13

标签: objective-c ios xml-parsing

我必须阅读并在我的书籍应用程序中显示多个角色对话框。我计划以下列格式在XML中存储字符对话数据:

<?xml version="1.0" encoding="UTF-8"?>
<Pages>
    <Page Id="P1">
        <T>Once upon a time in the deep jungle of sunderbun there lived a lazy and wicked Jackal.Once upon a time in the deep jungle of sunderbun there lived a lazy and wicked Jackal.</T>
        <A>This is audio file location.This is audio file location.This is audio file location.</A>
        <Dlg>
            <Dlg Id="C1D1" P="L" X="" Y="" W="" H="">
                <T>This is Character 1 Dialogue 1.This is Character 1 Dialogue 1.</T>
                <A>This is audio file location.This is audio file location.</A>
            </Dlg>
            <Dlg Id="C1D2" P="R" X="" Y="" W="" H="">
                <T>This is Character 1 Dialogue 2.This is Character 1 Dialogue 2.</T>
                <A>This is audio file location.This is audio file location.</A>
            </Dlg>
            <Dlg Id="C2D1" P="L" X="" Y="" W="" H="">
                <T>This is Character 2 Dialogue 1.This is Character 2 Dialogue 1.</T>
                <A>This is audio file location.This is audio file location.</A>
            </Dlg>
        </Dlg>
    </Page>
    <Page Id="P2">
        <T>Once upon a time in the deep jungle of sunderbun there lived a lazy and wicked Jackal.Once upon a time in the deep jungle of sunderbun there lived a lazy and wicked Jackal.</T>
        <A>This is audio file location.This is audio file location.This is audio file location.</A>
        <Dlg>
            <Dlg Id="C1D1" P="R" X="" Y="" W="" H="">
                <T>This is Character 1 Dialogue 1.This is Character 1 Dialogue 1.</T>
                <A>This is audio file location.This is audio file location.</A>
            </Dlg>
            <Dlg Id="C2D1" P="L" X="" Y="" W="" H="">
                <T>This is Character 2 Dialogue 1.This is Character 2 Dialogue 1.</T>
                <A>This is audio file location.This is audio file location.</A>
            </Dlg>
            <Dlg Id="C2D2" P="R" X="" Y="" W="" H="">
                <T>This is Character 2 Dialogue 2.This is Character 2 Dialogue 2.</T>
                <A>This is audio file location.This is audio file location.</A>
            </Dlg>
        </Dlg>
    </Page>
</Pages>

从XML结构中可以看出,会有多个页面,每个页面都有多个字符,每个字符都有多个对话框。这个XML的大小可能大约为250 KB(不确定这对iPhone / iPad来说太大了)。我打算使用GDataXML来解析这个XML并将其存储为以下模型对象:

#import <Foundation/Foundation.h>

typedef enum {
    Left,
    Right
} DialoguePosition;

@interface Dialogue : NSObject{
    NSString *_id;
    int _frameX;
    int _frameY;
    int _frameWidth;
    int _frameHeight;
    DialoguePosition _dialoguePosition;
    NSString *_dialogueText;
    NSString *_dialogueAudioLocation;
}
@property (nonatomic, copy) NSString *id;
@property (nonatomic, assign) int frameX;
@property (nonatomic, assign) int frameY;
@property (nonatomic, assign) int frameWidth;
@property (nonatomic, assign) int frameHeight;
@property (nonatomic, assign) DialoguePosition dialoguePosition;
@property (nonatomic, copy) NSString *dialogueText;
@property (nonatomic, copy) NSString *dialogueAudioLocation;

- (id)initWithName:(NSString *)id frameX:(int)frameX frameY:(int)frameY frameWidth:(int)frameWidth frameHeight:(int)frameHeight dialoguePosition:(DialoguePosition)dialoguePosition dialogueText:(NSString *)dialogueText dialogueAudioLocation:(NSString *)dialogueAudioLocation;

@end

我应该使用Property List而不是XML或XML应该没问题吗?我需要通过传递Page ID&amp; amp;对话ID。为了实现这一点,我应该在NSDictionary中存储Dialogue模型对象还是应该采用其他更好的方法。我很感激您对我应采取的方法的建议。

0 个答案:

没有答案