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];
}
/*
<Party>
<Player>
<Name>Butch</Name>
<Level>1</Level>
<Class>Fighter</Class>
</Player>
<Player>
<Name>Shadow</Name>
<Level>2</Level>
<Class>Rogue</Class>
</Player>
<Player>
<Name>Crak</Name>
<Level>3</Level>
<Class>Wizard</Class>
</Player>
</Party>
*/
//接收完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//解析xml
GdataxMLDocument *doc=[[GdataxMLDocument alloc]initWithData:netData options:0 error:Nil];
// NSLog(@"doc is %@",doc.rootElement.XMLString);
NSArray * partyMembers = [doc.rootElement nodesForXPath:@"//Party/Player" error:nil];
//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);
}
原文链接:https://www.f2er.com/xml/298352.html