使用KissXml解析xml

前端之家收集整理的这篇文章主要介绍了使用KissXml解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对于工程中的文件text.xml内容如下:

<?xml version="1.0" encoding="utf-8"?>

<XML>

<USER>Angel</USER>

<TITLE>Parser xml</TITLE>

<CONTENT>test Parser Xml With Kiss Xml</CONTENT>

</XML>


需要使用到kissxml解析xml

首先获取text.xml的url

NSString *path = [[NSBundle mainBundle] pathForResource:@"xmlData" ofType:@"xml"];

NSData *data = [[NSData alloc] initWithContentsOfFile:path];


接下来使用kissxml解析xml,

-(void)parsedDataFromData:(NSData *)data{
	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
	DDXMLDocument *doc = [[DDXMLDocument alloc] initWithData:data options:0 error:nil];
	
	/////解析
	NSArray *items = [doc nodesForXPath:kXML error:nil];
	
	for (DDXMLElement *obj in items) {
		xmlData *data = [[xmlData alloc] init];
		
		DDXMLElement *aUser = [obj elementForName:KUSER];
		if(aUser)
			data.user = aUser.stringValue;
		DDXMLElement *aTitle = [obj elementForName:KTITLE];
		if(aTitle)
			data.title = aTitle.stringValue;
		DDXMLElement *content = [obj elementForName:KCONTENT];
		if(content)
			data.content = content.stringValue;
		NSDictionary *XmlDictionary;
		XmlDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
						 data.user,@"kUsers",data.title,@"kTitles",data.content,@"kContents",nil];
		[self performSelectorInBackground:@selector(parsedXml:) withObject:XmlDictionary];
		[data release];
	}
	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	[doc release];
}
KissXml解析xml

猜你在找的XML相关文章