ios xmlnode html,iOS GDataXMLNode解析和写入XML文件

最近工程中有对XML的解析和写入,便稍微了解了一点。

SAX:sax解析是系统的解析,支持解析,但不支持写入,优点是解析的占用内存低,效率高,系统的便是这种方式。

DOM:dom解析是支持解析和写入的,但是它是将文档整个的读取到内存中,再进行操作,所以占用资源比较多。因为工程中功能的需要解析和写入,便使用的是谷歌的一个XML解析类GDataXMLNode。

首先如果你是没接触过xml的新手,那简单的了解一下,xml里有节点,然后每个节点可能有多个属性,然后每个属性都有一个值。

例如 ,PrintId是节点,ID是属性,后面的0000000是值,你可以这样简单的理解,这是我们当前的方法解决的xml样式。

一 解析

//方法的调用

[self readXmlNodeAttributeWithXmlFileName:@"Config" nodePath:@"Application/Config/PrintId" nodeAttributeName:@"Value"];

//根据参数的样式配置的解析方法。 xml文件都是单字节。

- (NSString *)readXmlNodeAttributeWithXmlFileName:(NSString *)xmlFileName nodePath:(NSString *)nodePath nodeAttributeName:(NSString *)nodeAttributeName{

@try {

//获得文件路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *xmlPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xml",xmlFileName]];

//将文件转换为data类型

NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];

//构建document文档对象(options预留参数)

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];

NSArray *nodeList = [nodePath componentsSeparatedByString:@"/"];

GDataXMLElement *currentNodes;

for (int i = 0; i < nodeList.count; i++) {

if (i == 0) {

currentNodes = [doc rootElement];

} else {

currentNodes = [[currentNodes elementsForName:nodeList[i]] objectAtIndex:0];

}

// 如果当前节点不存在,则返回默认值

if (currentNodes == nil) {

return @"";

}

}

NSString *result = [[currentNodes attributeForName:nodeAttributeName] stringValue];

return result;

} @catch (NSException *exception) {

NSLog(@"%@",exception);

}

}

二 写入

//方法的调用

[self writeXmlNodeAttributeWithXmlFileName:@"Config" nodePath:@"Application/Config/DeviceType" nodeAttributeName:@"OOOOOOOOOOO" nodeAttributeValue:@"111111"];

- (void)writeXmlNodeAttributeWithXmlFileName:(NSString *)xmlFileName nodePath:(NSString *)nodePath nodeAttributeName:(NSString *)nodeAttributeName nodeAttributeValue:(NSString *)nodeAttributeValue{

@try {

//获得文件路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *xmlPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xml",xmlFileName]];

//将文件转换为data类型

NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];

//构建document文档对象(options预留参数)

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];

NSArray *nodeList = [nodePath componentsSeparatedByString:@"/"];

GDataXMLElement *currentNodes;

GDataXMLElement *cacheNodes;

GDataXMLElement *rootEle = [doc rootElement];

for (int i = 0; i < nodeList.count; i++) {

if (i == 0) {

currentNodes = [doc rootElement];

} else {

cacheNodes = [[currentNodes elementsForName:nodeList[i]] objectAtIndex:0];

if (cacheNodes != nil) {

currentNodes = cacheNodes;

}else {

//如果该节点没有 则创建一个新的节点并加入当前节点中

GDataXMLElement *newNode = [GDataXMLElement elementWithName:nodeList[i]];

[currentNodes addChild:newNode];

currentNodes = newNode;

continue;

}

}

}

//给当前节点增加属性和值

[currentNodes addAttribute:[GDataXMLElement elementWithName:nodeAttributeName stringValue:nodeAttributeValue]];

GDataXMLDocument *xmlDoc = [[GDataXMLDocument alloc]initWithRootElement:rootEle];

NSData *data = [xmlDoc XMLData];

//此处可将二进制转成string样式打印

// NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

//将数据写入

[data writeToFile:xmlPath atomically:YES];

} @catch (NSException *exception) {

NSLog(@"%@",exception);

}

}

ps:我在解析xml文件时候,碰到很少部分的xml文件出现解析不了的情况,GDataXMLNode会直接报错,找了比较长时间 找到了这篇帖子

http://blog.csdn.net/yangfanacc/article/details/20640227

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值