我需要从给定的xml格式检索数据。我如何检索数据? Here is a link to the XML。无法从xml格式检索数据
回答
首先,添加委托的URLConnection并在你的头文件pasrsing:
代表补充说明的是:
NSURLConnectionDelegate
NSXMLParserDelegate
,那么你必须请拨打XML网址:
NSURL *url = [NSURL URLWithString:URLString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:10];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
responseData = [[NSMutableData data] retain];
}
实现NSURLConnectionDelegate
方法,这将是这样的:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData");
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError = %@",[error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
xmlParser= [[NSXMLParser alloc]initWithData:responseData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];
[responseData release];
[connection release];
}
然后添加XMLPARSER委托方法:
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//The tags in the xml data, the opening tags
}
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string {
//The data in tags
}
-(void)parser:(NSXMLParser*)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName {
//The end of each tags
}
下面是用于处理XML数据的一个很好的教程:Tutorial
am gettng响应问题是当我用xml解析器解析时代表我遇到问题 – Sowji
解析器didStartElement currentElement = [elementName copy];如果([elementName isEqualToString:@“MainMenu”]) \t { sub = [[NSMutableString alloc] init]; subsub = [[NSMutableString alloc] init]; sub1 = [[NSMutableString alloc] init]; } – Sowji
你在这里有什么问题,一切看起来都正确。 – Scar
- 1. 无法检索从给定的格式
- 2. 无法从数据库检索数据
- 3. 无法从xml中检索数据以放置在数组中
- 4. 从WebAPI检索JSON格式的Xml和字符串数据
- 5. 从xml检索数据(格式错误?)c#
- 6. 无法从字符串检索/复制xml数据到文件
- 7. 格式异常从数据网格视图检索数据
- 8. 无法从Facebook apprequest中检索数据
- 9. 无法从数据库检索值
- 10. 无法从PHP类中检索数据
- 11. 无法从Strawpoll API检索数据
- 12. 无法从SQLite数据库检索值
- 13. 无法从Firebase正确检索数据
- 14. 无法从SQLite中检索数据
- 15. 无法从Firebase数据库检索值
- 16. 脸书“无法从URL检索数据”
- 17. 无法从Cassandra检索数据
- 18. 无法检索从SQL Server数据库
- 19. Facebook无法从URL中检索数据
- 20. 无法从套件中检索数据
- 21. 无法从数据库检索项目
- 22. 无法从PHP检索AJAX数据
- 23. 无法从Firebase数据库检索值
- 24. 无法从MySQL表中检索数据
- 25. CloudKit - 无法从iCloud检索数据
- 26. 无法从kivy的jsonstore检索数据
- 27. 无法从SharedPreferences检索数据?
- 28. Angular2 - 无法从API检索数据
- 29. 无法从互联网检索数据
- 30. 无法从Firebase检索数据
HTTPS ://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/XMLParsing.html – BBog