现在的位置: 首页 > 综合 > 正文

[iPhone初级]IOS自带NSXMLParser对xml文件的解析

2018年02月13日 ⁄ 综合 ⁄ 共 3638字 ⁄ 字号 评论关闭

很多人都知道利用第三方来解析xml文件,这里我们介绍一下如何利用IOS自带的NSXMLParser来进行xml解析

我们要解析的文件如下:

[html] view
plain
copy

  1. <span style="font-size:18px;"><?xml version="1.0" ?>  
  2. <theresponse>  
  3.  <status>OK</status>  
  4.  <pricing currency="USD" symbol="$">  
  5.    <price class="items">24.00</price>  
  6.    <price class="shipping">6.00</price>  
  7.    <price class="tax">1.57</price>  
  8.  </pricing>  
  9. </theresponse></span>  

利用NSXMLParser解析,我们需要在需要解析的类中加入NSXMLParserDelegate,这其中有三个比较重要的方法,一个是

-(void)parser :( NSXMLParser *)parser didStartElement :( NSString *)elementName namespaceURI :( NSString *)namespaceURI qualifiedName :( NSString *)qName attributes :( NSDictionary *)attributeDict

这个方法主要是找到开始元素,例如我们要解析<status>,这里elementName就可以定位到status这里

-(void)parser :( NSXMLParser *)parser foundCharacters :( NSString *)string

这个方法解析的是中间的值<status>OK</status>这里的OK就是这里的值

-(void)parser :( NSXMLParser *)parser didEndElement :( NSString *)elementName namespaceURI :( NSString *)namespaceURI qualifiedName :( NSString *)qName

最后一个方法就是定位到</status>这个节点,在这个节点我们就可以把我们解析出来的数据存入NSDictionary或是NSArray中去了

我们还是来看一下代码吧

ItemParser.h

#import <Foundation/Foundation.h>
@interface ItemParser : NSXMLParser <NSXMLParserDelegate>
@property (readonly) NSDictionary *itemData;
@end

ItemParser.m

#import "ItemParser.h"
@implementation ItemParser {
    NSMutableDictionary *_itemData;
    NSMutableDictionary *_attributesByElement;
    NSMutableString *_elementString;
}
-(NSDictionary *)itemData{
    return [_itemData copy];
}
-(void)parserDidStartDocument :( NSXMLParser *)parser{
    _itemData = [[NSMutableDictionary alloc] init];
    _attributesByElement = [[NSMutableDictionary alloc] init];
    _elementString = [[NSMutableString alloc] init];
}
-(void)parser :( NSXMLParser *)parser didStartElement :( NSString *)elementName namespaceURI :( NSString *)namespaceURI qualifiedName :( NSString *)qName attributes :( NSDictionary *)attributeDict{
    // Save the attributes for later.
    if (attributeDict) [_attributesByElement setObject:attributeDict forKey:elementName];
    // Make sure the elementString is blank and ready to find characters
    [_elementString setString:@""];
}
-(void)parser :( NSXMLParser *)parser foundCharacters :( NSString *)string{
    // Save foundCharacters for later
    [_elementString appendString:string];
}
-(void)parser :( NSXMLParser *)parser didEndElement :( NSString *)elementName namespaceURI :( NSString *)namespaceURI qualifiedName :( NSString *)qName{
    if ([elementName isEqualToString:@"status"]){
        // Element status only contains a string i.e. "OK"
        // Simply set a copy of the element value string in the itemData dictionary
        [_itemData setObject:[_elementString copy] forKey:elementName];
    } else if ([elementName isEqualToString:@"pricing"]) {
        // Pricing has an interesting attributes dictionary 
        // So copy the entries to the item data
        NSDictionary *attributes = [_attributesByElement objectForKey:@"pricing"];
        [_itemData addEntriesFromDictionary:attributes];
    } else if ([elementName isEqualToString:@"price"]) {
        // The element price occurs multiple times.
        // The meaningful designation occurs in the "class" attribute.
        NSString *class = [[_attributesByElement objectForKey:elementName] objectForKey:@"class"];
        if (class) [_itemData setObject:[_elementString copy] forKey:class];
    }
    [_attributesByElement removeObjectForKey:elementName];
    [_elementString setString:@""];
}
-(void)parserDidEndDocument :( NSXMLParser *)parser{
    _attributesByElement = nil;
    _elementString = nil;
}
-(void)parser :( NSXMLParser *)parser parseErrorOccurred :( NSError *)parseError{
    NSLog(@"%@ with error %@",NSStringFromSelector(_cmd),parseError.localizedDescription);
}
-(BOOL)parse{
    self.delegate = self;
    return [super parse];
}
@end

这里解析后的结果:

{
    currency = USD;
    items = "24.00";
    shipping = "6.00";
    status = OK;
    symbol = "$";
    tax = "1.57";
}

好了,解析完毕,这里的还有一个重点就是copy,希望各位同学能多注意每个变量的记数。我自己也是JAVA做习惯了,过早释放,老是取不到值。呵呵,谨记。


转自:http://blog.csdn.net/kangkangz4/article/details/7616877




抱歉!评论已关闭.