当前位置: 首页 > 工具软件 > TBXML > 使用案例 >

ios-使用TBXML解析XML文件

缪嘉志
2023-12-01

ios平台除了可以使用NSXML解析XML文件外,还有TBXML框架也可以解析XML文件,TBXML采用DOM模式。

TBXML框架可以在网络上下载,网络地址为:https://github.com/71squared/TBXML   ,下载完成并解压后,将TBXML-Headers和TBXML-Code文件夹拷贝到工程中,由于TBXML依赖libz.dylib库,还需要在工程Framework中添加这个库。(注:如果工程使用ARC,在工程的.pch文件中添加宏定义:#define ARC_ENABLED)

1、例子Notes.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Notes>
    <Note id="1">
        <CDate>2012-12-21</CDate>
        <Content>iosBOOK_1</Content>
        <UserID>tony</UserID>
    </Note>
    <Note id="2">
        <CDate>2012-12-22</CDate>
        <Content>iosBOOK_2</Content>
        <UserID>tony</UserID>
    </Note>
    <Note id="3">
        <CDate>2012-12-23</CDate>
        <Content>iosBOOK_3</Content>
        <UserID>tony</UserID>
    </Note>
    <Note id="4">
        <CDate>2012-12-24</CDate>
        <Content>iosBOOK_4</Content>
        <UserID>tony</UserID>
    </Note>
    <Note id="5">
        <CDate>2012-12-25</CDate>
        <Content>iosBOOK_5</Content>
        <UserID>tony</UserID>
    </Note>
    <Note id="6">
        <CDate>2012-12-26</CDate>
        <Content>iosBOOK_6</Content>
        <UserID>tony</UserID>
    </Note>
</Notes>

2、编写专门解析XML文件的类(NotesTBXMLParser):

- (void)start
{
    _notes = [[[NSMutableArray alloc] init] autorelease];
    
    TBXML *tbxml = [[TBXML alloc] initWithXMLFile:@"Notes.xml" error:nil];
    TBXMLElement *root = tbxml.rootXMLElement;
    
    if (root)
    {
        TBXMLElement *noteElement = [TBXML childElementNamed:@"Note" parentElement:root];
        
        while (noteElement != nil)
        {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            
            TBXMLElement *CDateElement = [TBXML childElementNamed:@"CDate" parentElement:noteElement];
            if (CDateElement != nil)
            {
                NSString *CDate = [TBXML textForElement:CDateElement];
                [dict setValue:CDate forKey:@"CDate"];
            }
            
            TBXMLElement *ContentElement = [TBXML childElementNamed:@"Content" parentElement:noteElement];
            if (ContentElement != nil)
            {
                NSString *Content = [TBXML textForElement:ContentElement];
                [dict setValue:Content forKey:@"Content"];
            }
            
            TBXMLElement *UserIDElement = [TBXML childElementNamed:@"UserID" parentElement:noteElement];
            if (UserIDElement != nil)
            {
                NSString *UserID = [TBXML textForElement:UserIDElement];
                [dict setValue:UserID forKey:@"UserID"];
            }
            
            NSString *_id = [TBXML valueOfAttributeNamed:@"id" forElement:noteElement];
            [dict setValue:_id forKey:@"id"];
            
            [_notes addObject:dict];
            
            noteElement = [TBXML nextSiblingNamed:@"Note" searchFromElement:noteElement];

            [dict release];
        }
        
        NSLog(@"解析完成!");
        // 使用KVO模式,通知中心机制
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadXMLData" object:_notes userInfo:nil];
        
        [_notes removeAllObjects];
        _notes = nil;
    }
}

3、实际使用:

- (void)initObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getXMLData:) name:@"reloadXMLData" object:nil];
    
    // 开始解析xml文件
    NotesTBXMLParser *parser = [[NotesTBXMLParser alloc] init];
    [parser start];
    [parser release];
}

- (void)getXMLData:(NSNotification *)notification
{
    NSMutableArray *xmlData = [notification object];
    NSLog(@"xmlData:%@",xmlData);
}

至此,TBXML的介绍已经完毕,效果如下:

2014-08-30 12:57:46.031 test71[7145:60b] 解析完成!
2014-08-30 12:57:46.033 test71[7145:60b] xmlData:(
        {
        CDate = "2012-12-21";
        Content = "iosBOOK_1";
        UserID = tony;
        id = 1;
    },
        {
        CDate = "2012-12-22";
        Content = "iosBOOK_2";
        UserID = tony;
        id = 2;
    },
        {
        CDate = "2012-12-23";
        Content = "iosBOOK_3";
        UserID = tony;
        id = 3;
    },
        {
        CDate = "2012-12-24";
        Content = "iosBOOK_4";
        UserID = tony;
        id = 4;
    },
        {
        CDate = "2012-12-25";
        Content = "iosBOOK_5";
        UserID = tony;
        id = 5;
    },
        {
        CDate = "2012-12-26";
        Content = "iosBOOK_6";
        UserID = tony;
        id = 6;
    }
)

 类似资料: