• XML专题:使用NSXMLParser解析xml文件


    使用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];

    http://blog.csdn.net/z251257144/article/details/7175634

  • 相关阅读:
    Linux上vi(vim)编辑器使用教程
    什么是servlet
    探索ArrayList自动改变size真相
    十种算法
    二级缓存:EHCache的使用
    Lucene:基于Java的全文检索引擎简介
    简述 Hibernate 和 JDBC 的区别、优缺点
    Spring学习笔记
    如何复制表结构、如何复制表数据:
    破解安装 SecureCRT 7.0.2 for mac完美破解版,mac secureCRT , apple secureCRT
  • 原文地址:https://www.cnblogs.com/geek6/p/3950949.html
Copyright © 2020-2023  润新知