结构化特征
DOM:Document Object Model 文档对象模型 有W3C 推荐
一次读取整个文档,整颗DOM树常驻内存,导致系统开销过大
SAX :Simple API for XML 并不是其推荐的标准 但是整个XML 行业的事实规范
8.4.4 使用GDataXML解析XML文档
GDataXML 开源 对libxml2的包装 支持解析和修改 支持XPath
1.添加libxml2.dylib
2.添加/usr/include/libxml2 搜索路径
BuildSettings-Header Search Paths
PS:不支持ARC 需要手动关闭 -fno-objc-arc
下载GDataXML源代码
https://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/
http://down.51cto.com/data/619375
#import"GDataXMLNode.h"
#import"MyBook.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUITextField*siteFild;
@end
@implementationViewController
//GDataXML解析
-(NSArray*)parseXML:(NSString*)xmlName
{
NSString* path=[[NSBundlemainBundle]pathForResource:xmlNameofType:@"xml"];
NSFileHandle* file=[NSFileHandlefileHandleForReadingAtPath:path];
NSData* data=[filereadDataToEndOfFile];
GDataXMLDocument* doc=[[GDataXMLDocumentalloc]initWithData:dataoptions:0error:nil];
//如果需要根据XML字符串来初始化GDataXMLDocument对象,则调用如下代码
// GDataXMLDocument* doc=[[GDataXMLDocument alloc]initWithXMLString:NSXMLStr options:0 error:nil];
//获取根元素也就是获取元素
GDataXMLElement* rootELement=[docrootElement];
NSArray* bookElements=[rootELementelementsForName:@"book"];
//初始化一个可变数组,用于存储将要获取的所有元素组成的集合
NSMutableArray* books=[[NSMutableArrayalloc]init];
for(GDataXMLElement* bookElementinbookElements) {
NSIntegerbookID=[[[bookElementattributeForName:@"id"]stringValue]integerValue] ;
NSString* title=[[[bookElementelementsForName:@"title"]objectAtIndex:0]stringValue];
NSString* author=[[[bookElementelementsForName:@"author"]
objectAtIndex:0]stringValue];
NSString* remark=[[[bookElementelementsForName:@"remark"]objectAtIndex:0]stringValue];
MyBook* book=[[MyBookalloc]init];
book.bookID=bookID;
book.author=author;
book.title=title;
book.remark=remark;
[booksaddObject:book];
}
return[bookscopy];
}
8.4.5 使用GDataXML生成 修改XML文档
//
// ViewController.m
// 5-8-1-XML
//
// Created by童康on 16/5/8.
// Copyright © 2016年童康. All rights reserved.
//
#import"ViewController.h"
#import"GDataXMLNode.h"
@interfaceViewController()
@property(weak,nonatomic)IBOutletUITextField*bookID;
@property(weak,nonatomic)IBOutletUITextField*bookName;
@property(weak,nonatomic)IBOutletUITextField*author;
@property(weak,nonatomic)IBOutletUITextField*remark;
@property(nonatomic,strong)NSString* xmlPath;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfxmlPath];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)add:(id)sender {
NSString* bookID=self.bookID.text;
NSString* bookName=self.bookName.text;
NSString* author=self.author.text;
NSString* remark=self.remark.text;
if(bookID.length>0&& bookName.length>0&& author.length>0&&remark.length>0) {
//使用NSFileHandle对象根据文件路径获取到文件
NSFileHandle* file=[NSFileHandlefileHandleForReadingAtPath:self.xmlPath];
NSData* data=[filereadDataToEndOfFile];
GDataXMLDocument*doc;
//定义变量保存将要处理的XML文档对象
GDataXMLElement* rootEle;
if(data) {
//根据NSData对象初始化
doc=[[GDataXMLDocumentalloc]initWithData:dataoptions:0error:nil];
rootEle=doc.rootElement;
}
//如果XML文档还不存在,则需要新建文档
else
{
//创建元素
rootEle=[GDataXMLNodeelementWithName:@"books"];
GDataXMLElement* bookEle=[GDataXMLNodeelementWithName:@"id"stringValue:bookID];
//创建id属性,属性值为bookID
GDataXMLNode* attr=[GDataXMLNodeattributeWithName:@"id"stringValue:bookID];
[rootEleaddAttribute:attr];
//依次创建3个子元素
GDataXMLElement* titleEle=[GDataXMLNodeelementWithName:@"title"stringValue:bookName];
GDataXMLElement* authorEle=[GDataXMLNodeelementWithName:@"author"stringValue:author];
GDataXMLElement* remarkEle=[GDataXMLNodeelementWithName:@"remark"stringValue:remark];
//将3个子元素添加到bookEle元素中
[bookEleaddChild:titleEle];
[bookEleaddChild:authorEle];
[bookEleaddChild:remarkEle];
//将book元素添加入XML文档跟元素的子元素
[rootEleaddChild:bookEle];
//
if(!data) {
//以指定的根元素创建Document对象
doc=[[GDataXMLDocumentalloc]initWithRootElement:rootEle];
}
//将doc专为data
[doc.XMLDatawriteToFile:self.xmlPathatomically:YES];
self.bookID.text=nil;
self.bookName.text=nil;
self.author.text=nil;
self.remark.text=nil;
//创建并显示提示框
UIAlertView* alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"添加成功!"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alertshow];
}
}else{
UIAlertView* alert=[[UIAlertViewalloc]initWithTitle:@"提示"message:@"您必须为所有信息都输入有效的值"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alertshow];
}
}
-(NSString*)xmlPath{
//获取应用程序沙盒的Documents路径
NSArray* paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* documentsDirectory=[pathsobjectAtIndex:0];
NSLog(@"%@",documentsDirectory);
return[NSStringstringWithFormat:@"%@/books.xml",documentsDirectory];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end