IOS用TBXML 解析XML

TBXML下载地址: https://github.com/71squared/TBXML

这是我解析的XML(setting.xml),做个备忘

<root>

<setting>

<title>EventOn</title>

<layout>tile</title>

<headerStyle>Banner</headerStyle>

<color>颜色值</color>

<date>2013-02-13</date>

<location>位置</location>

</setting>

<!--  主菜单  -->

<module id="mainmenu" title="首页sdfsdfsd" icon="custom_icon.png">

<item title="地图ssfdafs" icon="close" target="EOMapViewController"/>

<item title="标题2" icon="close" target="ItemViewController"/>

<item title="标题3" icon="itemImage" target="ItemViewController"/>

</module>

<module id="map" title="地图" locationName="豪成大厦3A02" latitude="" longitude="">

</module>

</root>



TBXML.h TBXML.m
放到工程里就OK了

#import "TBXML.h"

@interface XXXClass : NSObject <NSXMLParserDelegate>


加入委托代理NSXMLParserDelegate


加载setting.xml

    NSError *error;

    TBXML *tbxml = [TBXML newTBXMLWithXMLFile:@"setting.xml" error:&error];

    TBXMLElement *rootElement = tbxml.rootXMLElement;

    if (error) {

        NSLog(@"%@ %@", [error localizedDescription], [error userInfo]);

    } else {

        TBXMLElement *element = [TBXML childElementNamed:@"module" parentElement:rootElement];

        [self dealMoudle:element];

    }


解析module结点信息


- (void) dealMoudle:(TBXMLElement *)element{

    do{

        NSString *value = [TBXML valueOfAttributeNamed:@"id" forElement:element];

        if([value isEqualToString:@"mainmenu"]){

            [self dealMainMenuMoudle:element];

        }

        if([value isEqualToString:@"map"]){

            

        }

    }while ((element = element->nextSibling));

}

- (void) dealMainMenuMoudle:(TBXMLElement *)_element{

    TBXMLElement *element = [TBXML childElementNamed:@"item" parentElement:_element];

    self.mainMenuArray = [[NSMutableArray alloc] init];

    self.mainMenuTitle = [TBXML valueOfAttributeNamed:@"title" forElement:_element];

    do{

        NSMutableDictionary *item = [[NSMutableDictionary alloc] init];

        [item setObject:[TBXML valueOfAttributeNamed:@"title" forElement:element] forKey:@"title"];

        [item setObject:[TBXML valueOfAttributeNamed:@"icon" forElement:element] forKey:@"icon"];

        [item setObject:[TBXML valueOfAttributeNamed:@"target" forElement:element] forKey:@"target"];

        [self.mainMenuArray addObject:item];

    }while((element = element->nextSibling));

}


好了,代码暂时就这么多,做个记录

你可能感兴趣的:(ios,xml,TBXML)