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 );
    } 

 

 
 
 
posted @ 2015-03-18 10:46  朱亚男  阅读(570)  评论(0编辑  收藏  举报