码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-02 12:00:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   os   io   使用   ar   for   文件   

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

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

标签:des   blog   http   os   io   使用   ar   for   文件   

原文地址:http://www.cnblogs.com/geek6/p/3950949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!