GDXM下载地址http://download.csdn.net/detail/zjcxy3150/4660938
将GdataxMLNode.h,GdataxMLNode.m,xpath三个文件拖入项目中(是MRC如果你的项目是ARC需要关闭该.m文件,变为MRC(加-fno-objc-arc))
找到Search Paths段,在Header Search Paths设置值为:/usr/include/libxml2
找到Linking段,在Other Linker Flags设置中填上:-lxml2
//苹果官方请求
NSURL *url=[NSURL URLWithString:@"http://www.raywenderlich.com/downloads/Party.xml"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
#pragma mark--
#pragma mark NSURLConnection代理方法
//管道建立成功
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
netData.length=0;
}
//接收数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[netData appendData:data];
}
@H_301_167@/*@H_301_167@<Party> @H_301_167@ <Player> @H_301_167@ <Name>Butch</Name> @H_301_167@ <Level>1</Level> @H_301_167@ <Class>Fighter</Class> @H_301_167@ </Player> @H_301_167@ <Player> @H_301_167@ <Name>Shadow</Name> @H_301_167@ <Level>2</Level> @H_301_167@ <Class>Rogue</Class> @H_301_167@ </Player> @H_301_167@ <Player> @H_301_167@ <Name>Crak</Name> @H_301_167@ <Level>3</Level> @H_301_167@ <Class>Wizard</Class> @H_301_167@ </Player> @H_301_167@</Party> @H_301_167@*/
//接收完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
@H_301_167@ //解析xmlGdataxMLDocument *doc=[[GdataxMLDocument alloc]initWithData:netData options:0 error:Nil];
@H_301_167@ // NSLog(@"doc is %@",doc.rootElement.XMLString);NSArray * partyMembers = [doc.rootElement nodesForXPath:@"//Party/Player" error:nil];
@H_301_167@ //NSLog(@"name is %@",names);
for (GdataxMLElement * partyMember in partyMembers) {
NSArray * names = [partyMember nodesForXPath:@"Name" error:nil];
if (names.count > 0) {
GdataxMLElement * firstName = (GdataxMLElement *)[names objectAtIndex:0];
NSLog(@"xml is %@",firstName.stringValue);
}
else
{
continue;
}
}
}
//管道建立失败
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"%@",error);
}