iOS学习之路 XML处理是本文要介绍的内容,主要介绍了XML文件的操作,具体详细内容我们看内容。不多说,直接进入话题。
解析不是utf-8的xml文件(尤其是中文网站的rss文件):
如果url中有中文:
- view plaincopy to clipboardprint?const char *str = [searchURL UTF8String];
- NSString *url = [NSString stringWithUTF8String:str];
- url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
- NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
- const char *str = [searchURL UTF8String];
- NSString *url = [NSString stringWithUTF8String:str];
- url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
- NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
gbk编码的xml文件:
- NSString *xmlStr = [[NSString alloc] initWithData:inData encoding:0x80000632];
- NSString *newXMLStr = [xmlStr stringByReplacingOccurrencesOfString:@"encoding=/"gbk/"" withString:@"encoding=/"UTF-8/""];
- NSXMLParser *nextParser = [[NSXMLParser alloc] initWithData:[newXMLStr dataUsingEncoding:NSUTF8StringEncoding]];
- NSString *xmlStr = [[NSString alloc] initWithData:inData encoding:0x80000632];
- NSString *newXMLStr = [xmlStr stringByReplacingOccurrencesOfString:@"encoding=/"gbk/"" withString:@"encoding=/"UTF-8/""];
- NSXMLParser *nextParser = [[NSXMLParser alloc] initWithData:[newXMLStr dataUsingEncoding:NSUTF8StringEncoding]];
在iOS中解析XML
以读文件的形式
iOS中的XML是event-driven模式
1:打开一个xml文件,读取内容到NSData中
2:调用NSXMLParse的nitWithData方法,并设置代理tweetParser.delegate = self;
3:调用回调函数
注意:xml文件的第一个tag必须要顶格写!
- <?xml version="1.0" encoding="UTF-8"?>
- <bookstore>
- <book catalog="Programming">
- <title lang="en">C++ Programming Language</title>
- <author>Bjarne Stroustrup</author>
- <year>1998</year>
- <price>98.0</price>
- </book>
- <book catalog="Networking">
- <title lang="en">TCP/IP Illustrated</title>
- <author>Richard Stevens</author>
- <year>1996</year>
- <price>56.0</price>
- </book>
- </bookstore>
- <?xml version="1.0" encoding="UTF-8"?>
- <bookstore>
- <book catalog="Programming">
- <title lang="en">C++ Programming Language</title>
- <author>Bjarne Stroustrup</author>
- <year>1998</year>
- <price>98.0</price>
- </book>
- <book catalog="Networking">
- <title lang="en">TCP/IP Illustrated</title>
- <author>Richard Stevens</author>
- <year>1996</year>
- <price>56.0</price>
- </book>
- </bookstore>
可以[NSXMLParser parserError]调用来得到错误的信息:
- NSXMLParserErrorDomain error 4: xml文件格式有问题(如上面)
- NSXMLParserErrorDomain error 5: 打开文件失败
在iOS 4.1中如果不显示实现NSXMLParseDelegate的话会有一条warning的提示,但是实行是没有问题的。
- //开始解析前,可以做一些初始话的工作
- - (void)parserDidStartDocument:(NSXMLParser *)parser {
- }
- - (void)parserDidStartDocument:(NSXMLParser *)parser {
- }
- //解析到一个开始tag,开始tag中可能会有properpies,例如<book catalog="Programming">
- //所有的属性都储存在attributeDict中
- - (void)parser:(NSXMLParser *)parser
- didStartElement:(NSString *)elementName
- namespaceURI:(NSString *)namespaceURI
- qualifiedName:(NSString *)qualifiedName
- attributes:(NSDictionary *)attributeDict {
- if ([elementName isEqual:@"book"]) {
- NSString *catalog = [attributeDict objectForKey:@"catalog"];
- }
- else if([bookTags containsObject:elementName]){
- }
- }
- - (void)parser:(NSXMLParser *)parser
- didStartElement:(NSString *)elementName
- namespaceURI:(NSString *)namespaceURI
- qualifiedName:(NSString *)qualifiedName
- attributes:(NSDictionary *)attributeDict {
- if ([elementName isEqual:@"book"]) {
- NSString *catalog = [attributeDict objectForKey:@"catalog"];
- }
- else if([bookTags containsObject:elementName]){
- }
- }
- //这时处理例如<title lang="en">C++ Programming Language</title>的情况
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- }
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
- }
- //处理到一个结束tag
- - (void)parser:(NSXMLParser *)parser
- didEndElement:(NSString *)elementName
- namespaceURI:(NSString *)namespaceURI
- qualifiedName:(NSString *)qName {
- }
- - (void)parser:(NSXMLParser *)parser
- didEndElement:(NSString *)elementName
- namespaceURI:(NSString *)namespaceURI
- qualifiedName:(NSString *)qName {
- }
- //xml解析结束
- - (void)parserDidEndDocument:(NSXMLParser *)parser {
- }
- - (void)parserDidEndDocument:(NSXMLParser *)parser {
- }
- //例子,这里要特别注意的是dataWithContentsOfFile后面一定要根绝对路径,而NSBundle能根据文件名来得到这个绝对路径。
- -(void) testXMLParse{
- NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"test.xml" ofType:nil inDirectory:nil];
- NSData *fileData = [NSData dataWithContentsOfFile:xmlPath];
- NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:fileData];
- [xmlParser setDelegate:self];
- BOOL result = [xmlParser parse];
- if (!result) {
- NSLog(@"The error is %@", [xmlParser parserError]);
- }
- //[xmlParser release];
- }
小结:iOS学习之路 XML处理的内容介绍完了,希望本文对你有所帮助!