B9、kissXML的简单使用

一、kissXML的github地址

https://github.com/robbiehanson/KissXML

二、使用前注意?

项目要导入libxml2.dylib。另运行时提示"libxml/tree.h"找不到,需要在项目的Build Settings搜索Header S,将Header Search Paths项设置为:/usr/include/libxml2。

三、kissXML的简单使用

1、xml文件如下:

2、几个方法使用举例

#import"DDXML.h"

#import"DDXMLElementAdditions.h"

@implementation ViewController

//解析XML

staticNSString *kXML =@"//District//Province";
- (void)viewDidLoad
{
    [super viewDidLoad];

   NSString *path =[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"ProvinceCity" ofType:@"xml"]]
   NSData *data = [[NSData alloc] initWithContentsOfFile:path];

   [self parsedDataFromData:data andProvince:@"江西省"];

[self parsedDataFromData:data];

}

//取特定省份下包含所有城市

-(void)parsedDataFromData:(NSData *)data andProvince:(NSString *)province{

DDXMLDocument *doc = [[DDXMLDocument alloc] initWithData:data options:0error:nil];

/////解析

NSArray *items = [docnodesForXPath:kXMLerror:nil];

for (DDXMLElement *objin items) {

        DDXMLNode *aUser = [obj attributeForName:@"Name"];//取属性Name的值

        if ([aUser.stringValueisEqualToString:province])

        {

            NSArray *CityLst = [obj elementsForName:@"City"];//取城市点点列表,保存到数组中

            if(CityLst.count>0)//第二层

            {

               for (DDXMLElement *citysin CityLst) {

                   DDXMLNode *citynode=[citys attributeForName:@"Name"];

                   NSLog(@"%@",citynode.stringValue);

                }

            }

        }

    }

}

//取所有省份及其下级节点,包含修改操作

- (void)parsedDataFromData:(NSData *)data{

DDXMLDocument *doc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil];

/////解析

NSArray *items = [doc nodesForXPath:kXML error:nil];

for (DDXMLElement *obj in items) {

DDXMLNode *aUser = [obj attributeForName:@"Name"];//取属性Name的值

       //[aUser setStringValue:@"haha!"];//修改属性节点的值

        [obj addAttribute:[DDXMLNodeattributeWithName:@"test"stringValue:@"wzh"]];//增加一个属性节点

        [obj addAttributeWithName:@"ttt"stringValue:@"343"];//再增加一个属性节点

       //[obj setStringValue:@"NewNode"];//设置当前节点的值

        DDXMLNode *newnode=[DDXMLNode elementWithName:@"newNode"];//设置一个新的节点

        [obj addChild:newnode];//给obj添加一个节点

DDXMLElement *newdxml = [[obj elementsForName:@"newNode"] objectAtIndex:0];//访问刚刚添加的节点

        [newdxml addAttributeWithName:@"nodetwo"stringValue:@"twovalue"];//给刚添加的节点增加增的下级节点,并且下级节点再一个属性值

        DDXMLNode *newnode2=[DDXMLNode elementWithName:@"newNode2" stringValue:@"hello!"];

        [newdxmladdChild:newnode2];

}

   //保存到沙盒目录下

   NSString *path =[[NSStringalloc]initWithFormat:@"%@/xmlData.xml", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]];

    NSString *result=[[NSStringalloc]initWithFormat:@"%@",doc];

    [resultwriteToFile:pathatomically:YESencoding:NSUTF8StringEncoding error:nil];
}

四、补充,如何把资源文件放到documents目录下。(验证未成功,求答案??)

    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_FILENAME];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DATABASE_FILENAME];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        ////NSAssert1(0, @"Failed to create writable database file with message ‘%@‘.", DATABASE_FILENAME );
    } 
时间: 2024-11-05 14:51:56

B9、kissXML的简单使用的相关文章

卓坠任何一件事只要心甘情愿总是能够变得简单

http://passport.baidu.com/?business&un=2015JS-SDK&un=%E4%B8%87%E8%8D%A3%E7%BE%8E%E5%A5%B3%E4%B8%8E%E7%94%B5%E8%AF%9D#0 http://passport.baidu.com/?business&un=2015JS-SDK&un=%E9%9C%8D%E5%B7%9E%E7%BE%8E%E5%A5%B3%E4%B8%8E%E7%94%B5%E8%AF%9D#0 h

VyOS--企业软路由推荐

今天给大家推荐一个好用的开源路由操作系统---VyOS,也是我个人非常喜欢的一款软路由器.VyOS 是基于 Debian GNU/Linux 的,提供了和其他诸如Cisco的IOS.Juniper的JUNOS类似的操作方式,配置起来感觉特别的舒服,尤其可以使用compare.rollback之类的命令,方便对比配置和错误回滚,深得我心. 由于之前用的Mikrotik RouterOS,在使用过程中逐渐发现了一些其性能和稳定性上的弊端,所以逐渐转向了VyOS,听一个资深linux老司机说,他曾经对

发个数据机房技术

http://www.yhd.com/marketing/tags/kfsar.com/kfsar/%E6%89%BE%E5%AD%A6%E7%94%9F%E5%A6%B9%E4%B8%8A%E9%97%A8%E5%85%A8%E5%A5%97%E6%9C%8D%E5%8A%A1%E2%96%93%E7%BE%8E%E5%A5%B3%E2%96%93%E7%94%B5%E8%AF%9D_185-8885-7572%E2%86%90%E3%80%912015%E5%B9%B403%E6%9C%88

演节目和那个

http://quote.hexun.com/stock/icb.aspx?code=1&name=%A8%8F%C1%D9%D2%CA%C4%C4%C0%EF%C2%F2%C3%D4%D2%A9%A3%D1%3A%A3%B6%A3%B9%A3%B5%A3%B2%A3%B5%A3%B6%A3%B7%A3%B1%A3%B7 http://quote.hexun.com/stock/icb.aspx?code=1&name=%A8%90%C8%D5%D5%D5%C4%C4%C0%EF%C2%F

可能接货功甫帖的染色

http://www.gettyimages.cn/newsr.php?thekeyword=%A8%80%C4%C4%C0%EF%D3%D0%C7%FA%C2%ED%B6%E0%C2%F4Q%A3%BA%A3%B2%A3%B0%A3%B8%A3%B6%A3%B0%A3%B6%A3%B7%A3%B5%A8%88 http://www.gettyimages.cn/newsr.php?thekeyword=%A8%7C%C4%C4%C0%EF%D3%D0%C2%F4%C7%FA%C2%ED%B6%

然后让合伙人如何让

http://ypk.39.net/search/all?k=%A6%D8%C7%EC%D1%F4%C4%C4%C0%EF%D3%D0%C3%D4%BB%E8%D2%A9%C2%F4Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%A9 http://ypk.39.net/search/all?k=%A8%80%B6%A8%CE%F7%C4%C4%C0%EF%D3%D0%C3%D4%BB%E8%D2%A9%C2%F4

mnjhgfdsfghjkl门口能接货规范的是

http://shike.gaotie.cn/zhan.asp?zhan=%A6%E7%BA%A3%C1%D6%D4%F5%C3%B4%B9%BA%C2%F2%C3%D4%D2%A9Q%A3%BA%A3%B8%A3%B6%A3%B3%A3%B9%A3%B0%A3%B2%A3%B9%A3%B6%A3%B2%A1%FA http://shike.gaotie.cn/zhan.asp?zhan=%A2%D9%C4%C2%C0%E2%D4%F5%C3%B4%B9%BA%C2%F2%C3%D4%D2%A9

兄,没结婚那个不

http://www.gettyimages.cn/newsr.php?thekeyword=%A1%F2%CD%A8%BB%AF%C4%C4%C0%EF%D3%D0%C2%F4%C5%E7%CE%ED%D0%CD%C3%D4%D2%A9Q%A3%BA%A3%B6%A3%B9%A3%B5%A3%B2%A3%B5%A3%B6%A3%B7%A3%B1%A3%B7%A8r http://www.gettyimages.cn/newsr.php?thekeyword=%A1%E2%B0%D7%C9%BD

。,i木有那还不如发给

http://ypk.39.net/search/all?k=%A1%F7%B7%F0%C9%BD%C4%C4%C0%EF%D3%D0%C2%F4%C3%D4%D2%A9Q%A3%BA%A3%B6%A3%B9%A3%B5%A3%B2%A3%B5%A3%B6%A3%B7%A3%B1%A3%B7%A8y http://ypk.39.net/search/all?k=%20%BD%AD%C3%C5%C4%C4%C0%EF%D3%D0%C2%F4%C3%D4%D2%A9Q%A3%BA%A3%B6%A3%