解析XML文件的方式有很多,今天我們介紹的是KissXML解析修改XML咒精。
大綱:
kissxml大綱.png
集成KissXML
使用Cocoapods集成
KissXML GitHub 路徑 https://github.com/robbiehanson/KissXML
cd 你的項目路徑
pod init
在項目中打開 Podfile 文件 添加 pod 'KissXML'
pod install
KissXML基本了解
DDXMLDocument
DDXMLDocument 是文檔
DDXMLElement
DDXMLElement 是元素節(jié)點
DDXMLNode
DDXMLNode 是子節(jié)點
XML文件的解析
解析XML文件的某個子節(jié)點
在項目中引入頭文件
'#import <KissXML.h>
///獲取文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
///把文件轉(zhuǎn)化成string
NSString *xmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
///使用kissXML解析
DDXMLDocument *xmlDocument = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
///找到xmlDocument下面所有的book元素
NSArray *arr = [xmlDocument nodesForXPath:@"http://book" error:nil];
for (int i = 0; i < arr.count; i ++) {
DDXMLElement *element = arr[i];
//分別為當前接點的名字、上一個接點(平級)烟瞧、下一個接點(平級)佑钾、上一個接點、第一個子節(jié)點
NSLog(@" 打印信息:%@--%@--%@--%@--%@--%@",element.name,element.previousSibling.name,element.nextSibling.name,element.previousNode.name,element.nextNode.name,element.parent.name);
}
獲取XML文件子節(jié)點數(shù)組
///獲取文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
///把文件轉(zhuǎn)化成string
NSString *xmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
///使用kissXML解析
DDXMLDocument *xmlDocument = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
///找到xmlDocument下面所有的book元素
NSArray *arr = [xmlDocument nodesForXPath:@"http://book" error:nil];
修改拼接XML文件
修改XML文件的屬性
///獲取文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
///把文件轉(zhuǎn)化成string
NSString *xmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
///使用kissXML解析
DDXMLDocument *xmlDocument = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
NSLog(@"XML元素的數(shù)量%ld",xmlDocument.childCount);
NSLog(@"XML元素的數(shù)組%@",xmlDocument.children);
///找到要修改的元素 修改內(nèi)容
for (DDXMLElement *element in xmlDocument.children) {
///找到shop元素
if ([element.name isEqualToString:@"shop"]) {
DDXMLElement *shopElement = element;
///shop標簽下的所有元素數(shù)組
NSArray *penChildren = shopElement.children;
for (DDXMLElement *childelement in penChildren) {
///找到pen元素
if ([childelement.name isEqualToString:@"pen"]) {
for (DDXMLNode *childnode in childelement.children) {
if ([childnode.name isEqualToString:@"type"]) {
[childnode setStringValue:@"---000---"];
}
if ([childnode.name isEqualToString:@"manufacturers"]) {
[childnode setStringValue:@"---xxxxx---"];
}
}
}
}
}
}
///修改完成之后需要把XML文件重新保存到新的地址 原文件修改的值不變
NSString *newPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"newXML.xml"];
NSFileManager *fileManager = [NSFileManager defaultManager];
///查看路徑是否存在
BOOL isFile = [fileManager fileExistsAtPath:newPath];
if (isFile) {
[fileManager removeItemAtPath:newPath error:nil];
}
NSMutableData *mutableData = [NSMutableData data];
NSData *data = [xmlDocument XMLData];
[mutableData appendData:data];
[mutableData writeToFile:newPath atomically:YES];
拼接XML文件
///獲取文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
///把文件轉(zhuǎn)化成string
NSString *xmlStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
///使用kissXML解析
DDXMLDocument *xmlDocument = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil];
for (DDXMLElement *ele in xmlDocument.children) {
if ([ele.name isEqualToString:@"shop"]) {
DDXMLElement *elementOne = [DDXMLElement elementWithName:@"one" stringValue:@"one"];
DDXMLElement *elementTwo = [DDXMLElement elementWithName:@"two" stringValue:@"two"];
[ele addChild:elementOne];
[ele addChild:elementTwo];
}
}
///修改完成之后需要把XML文件重新保存到新的地址 原文件修改的值不變
NSString *newPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"newXML.xml"];
NSFileManager *fileManager = [NSFileManager defaultManager];
///查看路徑是否存在
BOOL isFile = [fileManager fileExistsAtPath:newPath];
if (isFile) {
[fileManager removeItemAtPath:newPath error:nil];
}
NSMutableData *mutableData = [NSMutableData data];
NSData *data = [xmlDocument XMLData];
[mutableData appendData:data];
[mutableData writeToFile:newPath atomically:YES];