运用GDataXML 解析xml文件
首先引入libxml2.dylib框架,还有GDataXMLNode类
将要解析的xml示例:san.xml
<response>
<channelName>news</channelName>
<docList>
<docInfo>
<docChannel>悠嘻猴</docChannel>
<docTitle>别人都知道我的好</docTitle>
<docDescription></docDescription>
<docWebUrl>
http://www.cocoachina.com
</docWebUrl>
</docInfo>
<docInfo>
<docChannel>兔斯基</docChannel>
<docTitle>你不知道</docTitle>
<docDescription></docDescription>
<docWebUrl>
http://www.baidu.com
</docWebUrl>
</docInfo>
<docInfo>
<docChannel>炮炮兵</docChannel>
<docTitle>那又有什么用</docTitle>
<docDescription></docDescription>
<docWebUrl>
http://www.hao123.com
</docWebUrl>
</docInfo>
</docList>
</response>
解析大概过程:
NSError* error = nil;
非本地:(NSString *moreDocURLStr = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"XXXXXXXXXX.xml"] encoding:NSUTF8StringEncoding error:&error] autorelease];
)
本地:NSString *path = [[NSBundle mainBundle]pathForResource:@"san" ofType:@"xml"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
GDataXMLDocument *festivalDocument = [[[GDataXMLDocument alloc]initWithXMLString:fileContent options:0 error:&error] autorelease];
GDataXMLElement *rootElement = [moreDocument rootElement];
GDataXMLElement *docListElement = [[rootElement elementsForName:@"docList"]objectAtIndex:0];
NSArray *docInfoArray = [docListElement elementsForName:@"docInfo"];
for (int i=0; i<[docInfoArray count]; i++)
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
GDataXMLElement *onedocInfoElement = [docInfoArray objectAtIndex:i];
NSArray *onedocInfoArray = [onedocInfoElement children];
for (int j=0; j<[onedocInfoArray count]; j++)
{
GDataXMLNode *node = [onedocInfoArray objectAtIndex:j];
[dic setObject:[node stringValue] forKey:[node name]];
}
NSLog(@"%@",dic);
}
得到dic的输出结果是:
2011-01-13 17:38:43.296 san[436:207] {
docChannel = "悠嘻猴";
docDescription = "";
docTitle = "别人都知道我的好";
docWebUrl = "http://www.cocoachina.com";
}
2011-01-13 17:38:43.297san[436:207] {
docChannel = "兔斯基";
docDescription = "";
docTitle = "你不知道";
docWebUrl = "http://www.baidu.com";
}
2011-01-13 17:38:43.297 san[436:207] {
docChannel = "炮炮兵";
docDescription = "";
docTitle = "那又有什么用";
docWebUrl = "http://www.hao123.com";
}
在解析文件时小小经验教训总结:
(一)网络图片的解析和保存
解析的时候如果遇到图片要进行储存(到plist文件),那么就要把图片转换成为数据流,而且还有可能不是每一个字典中都有图片,输出的就是null,那么就要进行判断了。(以下代码不再出示xml文件示例)
GDataXMLElement *imagesElement = [[imageRootElement elementsForName:@"images"] objectAtIndex:0];
GDataXMLElement *imageElement = [[imagesElement elementsForName:@"image"] objectAtIndex:0];
GDataXMLElement *picurlElement = [[imageElement elementsForName:@"picurl"] objectAtIndex:0];
GDataXMLNode *picurlNode = [[picurlElement children]objectAtIndex:0];
if ([[picurlNode stringValue] isEqualToString:@"null"]) {
[dic setObject:@"" forKey:@"picurl"];
}else {
[dic setObject:[picurlNode stringValue] forKey:@"picurl"];
}
//将图片转换为NSDATA存入字典
NSString *ImgStr = [dic objectForKey:@"picurl"];
if (ImgStr==@"") {
[dic setObject:UIImagePNGRepresentation([UIImage imageNamed:@"xx.png"]) forKey:@"thumbnail"];(如果网络上没有解析到图片,那么就将本地的图片"xx.png"的数据流存进字典)
}
else {
NSURL *imgUrl = [NSURL URLWithString:ImgStr];
NSData *imageData = [NSData dataWithContentsOfURL:imgUrl];
[dic setObject:imageData forKey:@"thumbnail"];
}
(二)字符串操作:
NSString *UrlString1 = @"www.baidu.com";
NSString *UrlString2 = @"www.hao123.com";
1、替换:
NSString *String1 = [UrlString1 stringByReplacingOccurrencesOfString:@"com" withString:@"com.XXX"];
2、拼接:
NSString *String 2= [UrlString1 stringByAppendingString:UrlString2];
3、滤掉月、日,得到纯数字字符串:
NSRange monRange = [UrlString3rangeOfString:@"月"];
NSRange dayRange = [UrlString3 rangeOfString:@"日"];
NSString *dateString;
NSString *_dateString;
if (monRange.location!=NSNotFound)
{
NSString *monString = [dayString substringToIndex:monRange.location];(dayString就是过滤之前的字符串,monString是月前面的数字)
if (monRange.location==1)————这一步是判断“月“前面是一个字符的情况(0-9月)
{
dateString = [dayString substringToIndex:dayRange.location];
_dateString = [dateString substringFromIndex:(monRange.location)+1];(_dateString就是日前面的数字)
}
else if (monRange.location==2)————这一步是判断“月”前面是两个字符的情况(10-12月)
{
dateString = [dayString substringToIndex:dayRange.location];
_dateString = [dateString substringFromIndex:(monRange.location)+1];(_dateString就是日前面的数字)
}
}
4、将字符串根据某段字符对其进行分割保存到数组中,再对数组中的字符串分别进行处理:
NSString *firstConStr =@“http://www.hao123.com/dasifhia/fidsfhisa/aosfhsia/";
NSArray *firConArray = [firstConStrcomponentsSeparatedByString:@"/"];
输出firConArray:
2011-01-13 18:53:26.483 [263:207] (
"http:",
"",
"www.hao123.com",
dasifhia,
fidsfhisa,
aosfhsia,
""
)