现在开发大多数时候都是通过json数据进行服务器与客户端的数据传输,但有时候也会遇到利用XML字符串进行交互的情况,本文将对这一功能进行基础讲解。
1.准备
- 下载第三方库,将GDataXMLNode导入到工程里。
- 在Xcode-->Build Phases-->Link Binary With Libraries里面添加
libxml2.tbd
- 在Xcode-->Build Phases-->Complie Sources里面找到GDataXMLNode.m,双击Complier Flag区域,添加
-fno-objc-arc
字段 - 在Xcode-->Build Settings-->Header Search Paths里面添加
/usr/include/libxml2
项。 - 在Xcode-->Build Settings-->Other Linker Flags里面添加
-lxml2
项。
2.实现
- 拼接XML文件
//这是关于DLNA功能的DIDL-Lite标准字段值
-(void)appendXMLString
{
//添加根节点
GDataXMLElement * rootElement = [GDataXMLNode elementWithName:@"DIDL-Lite"];
//根节点到属性及值
GDataXMLElement * rootFirstArg = [GDataXMLNode attributeWithName:@"xmlns" stringValue:@"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"];
GDataXMLElement * rootSecondArg = [GDataXMLNode attributeWithName:@"xmlns:dc" stringValue:@"http://purl.org/dc/elements/1.1/"];
GDataXMLElement * rootThirdArg = [GDataXMLNode attributeWithName:@"xmlns:upnp" stringValue:@"urn:schemas-upnp-org:metadata-1-0/upnp/"];
GDataXMLElement * rootFourthArg = [GDataXMLNode attributeWithName:@"xmlns:dlna" stringValue:@"urn:schemas-dlna-org:metadata-1-0/"];
[rootElement addAttribute:rootFirstArg];
[rootElement addAttribute:rootSecondArg];
[rootElement addAttribute:rootThirdArg];
[rootElement addAttribute:rootFourthArg];
//添加item节点
GDataXMLElement * itemElement = [GDataXMLNode elementWithName:@"item"];
//添加item节点的属性及值
GDataXMLElement * itemFirstArg = [GDataXMLNode attributeWithName:@"id" stringValue:@""];
GDataXMLElement * itemSecondArg = [GDataXMLNode attributeWithName:@"parentID" stringValue:@"-1"];
GDataXMLElement * itemThirdArg = [GDataXMLNode attributeWithName:@"restricted" stringValue:@"1"];//1 or ture
[itemElement addAttribute:itemFirstArg];
[itemElement addAttribute:itemSecondArg];
[itemElement addAttribute:itemThirdArg];
//添加title节点
GDataXMLElement * titleElement = [GDataXMLNode elementWithName:@"dc:title"];
//设置title的值
[titleElement setStringValue:@"测试音频"];
//添加res节点,属性和值
GDataXMLElement * resElement = [GDataXMLNode elementWithName:@"res"];
GDataXMLElement * resFisrtArg = [GDataXMLNode attributeWithName:@"duration" stringValue:@"04:48"];
GDataXMLElement * resSecondArg = [GDataXMLNode attributeWithName:@"protocolInfo" stringValue:@"http-get:*::*"];
[resElement setStringValue:@"www.baidu.com"];
GDataXMLElement * upnpClassEle = [GDataXMLNode elementWithName:@"upnp:class"];
[upnpClassEle setStringValue:@"object.item.audioItem.musicTrack"];
//一层一层的往上加节点,构成xml结构
[resElement addAttribute:resFisrtArg];
[resElement addAttribute:resSecondArg];
[itemElement addChild:titleElement];
[itemElement addChild:resElement];
[itemElement addChild:upnpClassEle];
[rootElement addChild:itemElement];
NSString * rootStr = [rootElement XMLString];
NSLog(@"%@",rootStr);
GDataXMLDocument * doc = [[GDataXMLDocument alloc] initWithRootElement:rootElement];
[doc setVersion:@"1.0"];
[doc setCharacterEncoding:@"utf-8"];
NSString * docStr = [[NSString alloc] initWithData:doc.XMLData encoding:NSUTF8StringEncoding];
[self saveXMLToDocDir:docStr];
}
-(void)saveXMLToDocDir:(NSString *)doc
{
NSString * xml = [DOCDIR stringByAppendingPathComponent:@"test.xml"];
[[NSFileManager defaultManager] createFileAtPath:xml contents:[doc dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
NSLog(@"沙盒路径:%@",xml);
}
- 解析XML文件
-(NSString *)getXMLFile
{
NSString * filePath = [DOCDIR stringByAppendingPathComponent:@"test.xml"];
NSString * file = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
return file;
}
-(void)praseXMLStringWithFile
{
NSString * file = [self getXMLFile];
GDataXMLDocument * doc = [[GDataXMLDocument alloc] initWithXMLString:file options:0 error:nil];
//获取根节点
GDataXMLElement * rootElement = [doc rootElement];
NSLog(@"根节点的标签是:%@",[rootElement name]);
//获取所有item节点
NSArray * itemElementArray = [rootElement elementsForName:@"item"];
for (int i=0; i<itemElementArray.count; i++) {
GDataXMLElement * itemElment = itemElementArray[i];
//获取属性为parentID的值
NSString * itemFirstArg = [[itemElment attributeForName:@"parentID"] stringValue];
NSLog(@"itemFirstArg:%@",itemFirstArg);
//获取所有title节点
NSArray * dcTitleArray = [itemElment elementsForName:@"dc:title"];
for (int j=0; j<dcTitleArray.count; j++) {
GDataXMLElement * titleElement = dcTitleArray[j];
//获取title节点的值
NSString * title = [titleElement stringValue];
NSLog(@"title:%@",title);
}
}
}
完成。
附上DEMO。
给个赞呗~
网友评论