• 使用NSXMLParser解析xml文件


     1. 设置委托对象,开始解析
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];   //或者也可以使用initWithContentsOfURL直接下载文件,但是有一个原因不这么做:
        // It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
        // because it gives less control over the network, particularly in responding to connection errors.
        [parser setDelegate:self];
        [parser parse];
    
        2. 常用的委托方法
        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
                                    namespaceURI:(NSString *)namespaceURI
                                    qualifiedName:(NSString *)qName 
                                    attributes:(NSDictionary *)attributeDict;
        - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
                                    namespaceURI:(NSString *)namespaceURI 
                                    qualifiedName:(NSString *)qName;
        - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
        - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
    
        static NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";
    
        3.  应用举例
        - (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
        {
            NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
            [parser setDelegate:self];
            [parser setShouldProcessNamespaces:NO];
            [parser setShouldReportNamespacePrefixes:NO];
            [parser setShouldResolveExternalEntities:NO];
            [parser parse];
            NSError *parseError = [parser parserError];
            if (parseError && error) {
                *error = parseError;
            }
            [parser release];
        }
    
        - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
                                            qualifiedName:(NSString*)qName attributes:(NSDictionary *)attributeDict{
            // 元素开始句柄
            if (qName) {
                elementName = qName;
            }
            if ([elementName isEqualToString:@"user"]) {
                // 输出属性值
                NSLog(@"Name is %@ , Age is %@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"age"]);
            }
        }
    
        - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI 
                                            qualifiedName:(NSString *)qName
        {
            // 元素终了句柄
            if (qName) {
                   elementName = qName;
            }
        }
    
        - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
        {
            // 取得元素的text
        }
    
        NSError *parseError = nil;
        [self parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:&parseError];
    
    
  • 相关阅读:
    SharePoint 在文本编辑框中插入图片报错
    SharePoint 修改列表阀值
    SharePoint 获取SPListItem附件地址
    SharePoint 验证用户组是否存在当前用户方法
    SharePoint中查看用户登录IP与用户名
    SharePoint CAML中通过ID查找Lookup字段
    SharePoint 服务器修改密码(前端服务器与数据库服务器分开)
    20191128-1 总结
    康哲 20191121-1 每周例行报告
    对成员的感谢
  • 原文地址:https://www.cnblogs.com/jiewong/p/1925456.html
Copyright © 2020-2023  润新知