最近工程中有對XML的解析和寫入,便稍微了解了一點矫渔。
SAX:sax解析是系統(tǒng)的解析彤蔽,支持解析,但不支持寫入庙洼,優(yōu)點是解析的占用內(nèi)存低顿痪,效率高,系統(tǒng)的便是這種方式油够。
DOM:dom解析是支持解析和寫入的蚁袭,但是它是將文檔整個的讀取到內(nèi)存中,再進行操作石咬,所以占用資源比較多揩悄。因為工程中功能的需要解析和寫入,便使用的是谷歌的一個XML解析類GDataXMLNode鬼悠。
GDataXMLNode的下載和集成配置就不贅述虏束,可參考一下的鏈接
http://jingyan.baidu.com/article/ab69b270b57f302ca6189f11.html
首先如果你是沒接觸過xml的新手,那簡單的了解一下厦章,xml里有節(jié)點镇匀,然后每個節(jié)點可能有多個屬性,然后每個屬性都有一個值袜啃。
例如 <PrintId ID="00000000000" />,PrintId是節(jié)點汗侵,ID是屬性,后面的0000000是值群发,你可以這樣簡單的理解,這是我們當(dāng)前的方法解決的xml樣式晰韵。
一 解析
//方法的調(diào)用
[self readXmlNodeAttributeWithXmlFileName:@"Config" nodePath:@"Application/Config/PrintId" nodeAttributeName:@"Value"];
//根據(jù)參數(shù)的樣式配置的解析方法。 xml文件都是單字節(jié)熟妓。
- (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]];
//將文件轉(zhuǎn)換為data類型
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
//構(gòu)建document文檔對象(options預(yù)留參數(shù))
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];
}
// 如果當(dāng)前節(jié)點不存在雪猪,則返回默認值
if (currentNodes == nil) {
return @"";
}
}
NSString *result = [[currentNodes attributeForName:nodeAttributeName] stringValue];
return result;
} @catch (NSException *exception) {
NSLog(@"%@",exception);
}
}
二 寫入
//方法的調(diào)用
[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]];
//將文件轉(zhuǎn)換為data類型
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
//構(gòu)建document文檔對象(options預(yù)留參數(shù))
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 {
//如果該節(jié)點沒有 則創(chuàng)建一個新的節(jié)點并加入當(dāng)前節(jié)點中
GDataXMLElement *newNode = [GDataXMLElement elementWithName:nodeList[i]];
[currentNodes addChild:newNode];
currentNodes = newNode;
continue;
}
}
}
//給當(dāng)前節(jié)點增加屬性和值
[currentNodes addAttribute:[GDataXMLElement elementWithName:nodeAttributeName stringValue:nodeAttributeValue]];
GDataXMLDocument *xmlDoc = [[GDataXMLDocument alloc]initWithRootElement:rootEle];
NSData *data = [xmlDoc XMLData];
//此處可將二進制轉(zhuǎn)成string樣式打印
// NSString *content = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//將數(shù)據(jù)寫入
[data writeToFile:xmlPath atomically:YES];
} @catch (NSException *exception) {
NSLog(@"%@",exception);
}
}
ps:我在解析xml文件時候,碰到很少部分的xml文件出現(xiàn)解析不了的情況起愈,GDataXMLNode會直接報錯只恨,找了比較長時間 找到了這篇帖子
http://blog.csdn.net/yangfanacc/article/details/20640227