总结如下:
①、每个结点的属性都算作一个GDataXMLNode,每个结点算作一个GDataXMLElement ,生成XML文档的时候,使用GDataXMLElement的addChild方法,不停的迭代,可以构建出一个XML文档。
②、我觉得使用XPath来定位某个结点比较好,这样可以做到同级结点相对位置的无关性。 XPath的总结笔记见下一篇博客。
代码如下:
//
// ViewController.m
// sva_xia_8_4_1
//
//
#import "ViewController.h"
#import "GDataXMLNode.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//view被加载后执行的方法
//①、写
// if(![self writeMyXML])
// {
// NSLog(@"写出person.xml失败");
// return;
// }
//②、读 + 修改在这一步里全做完了
if (![self readMyXML])
{
NSLog(@"读入person.xml失败");
return;
}
//③、修改
//判断沙盒里Documents文件夹下my.xml文件是否存在,若不存在,首先创建这个文件
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentDirectory = [directoryPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/my.xml",documentDirectory];
NSLog(@"%@",filePath);
}
-(BOOL) readMyXML
{
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentDirectory = [directoryPaths objectAtIndex:0];
NSString *strXMLFilePath = [NSString stringWithFormat:@"%@/person.xml",documentDirectory];
NSString *strXML=[NSString stringWithContentsOfFile:strXMLFilePath encoding:NSUTF8StringEncoding error:nil];
if(nil==strXML)
{
NSLog(@"读取文件时找不到person.xml文件");
return NO;
}
GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithXMLString:strXML options:0 error:nil];
@try
{
//获取 /person/liji/age 结点的value值,并修改他
NSArray *aryNode=[doc nodesForXPath:@"/person/liji/age" error:nil];
NSUInteger iCount=aryNode.count;
if(iCount>0)
{
GDataXMLNode *node=(GDataXMLNode *)aryNode[0];
NSLog(@"获取到了/liji/age结点,其值为: %@",[node stringValue]);
[node setStringValue:@"88"];
NSLog(@"修改了/liji/age结点的值");
}
else
{
NSLog(@"XPath获取到的数组里没有元素");
return NO;
}
//获取 /person/lixi结点的id属性值,然后将其修改为222
NSArray *aryNode2=[doc nodesForXPath:@"/person/lixi" error:nil];
NSUInteger iCount2=aryNode2.count;
if(iCount2>0)
{
GDataXMLElement *node2=(GDataXMLElement *)aryNode2[0];
GDataXMLNode *node2_2=[node2 attributeForName:@"id"];
if(nil==node2_2)
{
NSLog(@"未获取到/person/lixi结点的id属性值");
return NO;
}
else
{
NSLog(@"获取到了/person/lixi结点id属性,其值为: %@",[node2_2 stringValue]);
[node2_2 setStringValue:@"2222"];
NSLog(@"修改了/person/lixi结点的id属性值");
}
}
else
{
NSLog(@"XPath获取到的数组里没有元素");
return NO;
}
//测试一下/person/liji 结点的 childCount的个数
NSArray *aryNode3=[doc nodesForXPath:@"/person/liji" error:nil];
NSUInteger iCount3=aryNode3.count;
if (iCount3>0)
{
GDataXMLNode *node3=aryNode3[0];
NSLog(@"/person/liji结点共有子结点: %lu 个",[node3 childCount]);
}
else
{
NSLog(@"未获取到/person/liji结点");
return NO;
}
}
@finally
{
NSData *xmlData=[doc XMLData];
NSArray *aryPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDocPath=[aryPath objectAtIndex:0];
NSString *strXMLPath=[NSString stringWithFormat:@"%@/person.xml",strDocPath];
if([xmlData writeToFile:strXMLPath atomically:YES])//会覆盖之前的文件
return YES; //这样写出,xml文件里没有换行
else
return NO;
}
}
-(BOOL) writeMyXML
{
//创建person结点
GDataXMLElement *ele_person=[GDataXMLNode elementWithName:@"person"];
//创建liji结点
GDataXMLElement *ele_liji=[GDataXMLNode elementWithName:@"liji"];
GDataXMLNode *attr_liji_id=[GDataXMLNode attributeWithName:@"id" stringValue:@"1"];
GDataXMLNode *attr_liji_sex=[GDataXMLNode attributeWithName:@"sex" stringValue:@"man"];
[ele_liji addChild:attr_liji_id];
[ele_liji addChild:attr_liji_sex];
GDataXMLElement *ele_liji_age=[GDataXMLNode elementWithName:@"age" stringValue:@"99"];
GDataXMLElement *ele_liji_length=[GDataXMLNode elementWithName:@"length" stringValue:@"174"];
[ele_liji addChild:ele_liji_age];
[ele_liji addChild:ele_liji_length];
[ele_person addChild:ele_liji];
//创建lixi结点
GDataXMLElement *ele_lixi=[GDataXMLNode elementWithName:@"lixi"];
GDataXMLNode *attr_lixi_id=[GDataXMLNode attributeWithName:@"id" stringValue:@"2"];
GDataXMLNode *attr_lixi_sex=[GDataXMLNode attributeWithName:@"sex" stringValue:@"man"];
[ele_lixi addChild:attr_lixi_id];
[ele_lixi addChild:attr_lixi_sex];
GDataXMLElement *ele_lixi_age=[GDataXMLNode elementWithName:@"age" stringValue:@"99"];
GDataXMLElement *ele_lixi_length=[GDataXMLNode elementWithName:@"length" stringValue:@"178"];
[ele_lixi addChild:ele_lixi_age];
[ele_lixi addChild:ele_lixi_length];
[ele_person addChild:ele_lixi];
//创建文档对象
GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithRootElement:ele_person];
[doc setVersion:@"1.0"];
[doc setCharacterEncoding:@"utf-8"];
NSData *xmlData=[doc XMLData];
NSArray *aryPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDocPath=[aryPath objectAtIndex:0];
NSString *strXMLPath=[NSString stringWithFormat:@"%@/person.xml",strDocPath];
if([xmlData writeToFile:strXMLPath atomically:YES])//会覆盖之前的文件
return YES; //这样写出,xml文件里没有换行
else
return NO;
}
-(BOOL) isFileExistsAtPath:(NSString *) filePath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL bFlagForIsDirectory=YES;
BOOL bFileExists=NO;
if([fileManager fileExistsAtPath:filePath isDirectory:&bFlagForIsDirectory])
{
if(!bFlagForIsDirectory)
bFileExists=YES;
}
return bFileExists;
}
-(BOOL) isCreateFileIfNotExistSucceed:(NSString *) filePath isAlreadyExist:(BOOL *)pbIsAlreadyExist
{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL bFlagForIsDirectory=YES;
BOOL bFileExists=NO;
if([fileManager fileExistsAtPath:filePath isDirectory:&bFlagForIsDirectory])
{
if(!bFlagForIsDirectory)
bFileExists=YES;
}
if(bFileExists)
{
*pbIsAlreadyExist=YES;
return YES;
}
else //如果不存在 ,就创建文件
{
*pbIsAlreadyExist=NO;
return [fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end