iOS编程将xml转为object使用

xml文件结构如下:

<?xml version="1.0" standalone="no"?>
<root>
  <status>
    <code>0</code>
    <msg>正确</msg>
  </status>
  <data>
    <picture_num>6</picture_num>
    <picture_list>
      <picture_name>img1</picture_name>
      <picture_scope>1</picture_scope>
      <picture_sign>da16641a9dd3d0ed099ac04c6823826c7bde5d61</picture_sign>
      <is_cover>0</is_cover>
      <width>1920</width>
      <height>1200</height>
      <img_src>http://192.168.40.1/01.jpg</img_src>
      <m_width>592</m_width>
      <m_height>370</m_height>
      <pic_big_src>http://192.168.40.1/01.jpg</pic_big_src>
    </picture_list>
    <picture_list>
      <picture_name>img2</picture_name>
      <picture_scope>1</picture_scope>
      <picture_sign>7361946e8bcf21193817d71425f2bf78fb25b360</picture_sign>
      <is_cover>0</is_cover>
      <width>1920</width>
      <height>1200</height>
      <img_src>http://192.168.40.1/02.jpg</img_src>
      <m_width>592</m_width>
      <m_height>370</m_height>
      <pic_big_src>http://192.168.40.1/02.jpg</pic_big_src>
    </picture_list>
    <picture_list>
      <picture_name>img3</picture_name>
      <picture_scope>1</picture_scope>
      <picture_sign>c43df3a2be33ec2b9d0c358f9cee061f50d68460</picture_sign>
      <is_cover>0</is_cover>
      <width>1920</width>
      <height>1200</height>
      <img_src>http://192.168.40.1/03.jpg</img_src>
      <m_width>592</m_width>
      <m_height>370</m_height>
      <pic_big_src>http://192.168.40.1/03.jpg</pic_big_src>
    </picture_list>
    <picture_list>
      <picture_name>img4</picture_name>
      <picture_scope>1</picture_scope>
      <picture_sign>d6cae9083ba9d7f35dc1ce5721f30cf184600c40</picture_sign>
      <is_cover>0</is_cover>
      <width>1920</width>
      <height>1200</height>
      <img_src>http://192.168.40.1/04.jpg</img_src>
      <m_width>592</m_width>
      <m_height>370</m_height>
      <pic_big_src>http://192.168.40.1/04.jpg</pic_big_src>
    </picture_list>
    <picture_list>
      <picture_name>img5</picture_name>
      <picture_scope>1</picture_scope>
      <picture_sign>697d423ae23f0d523d532ef5fc040c812240c741</picture_sign>
      <is_cover>0</is_cover>
      <width>1920</width>
      <height>1200</height>
      <img_src>http://192.168.40.1/05.jpg</img_src>
      <m_width>592</m_width>
      <m_height>370</m_height>
      <pic_big_src>http://192.168.40.1/05.jpg</pic_big_src>
    </picture_list>
    <picture_list>
      <picture_name>img6</picture_name>
      <picture_scope>1</picture_scope>
      <picture_sign>6a97ad9e4e18f7176150617cb71120d33ce4df60</picture_sign>
      <is_cover>0</is_cover>
      <width>1920</width>
      <height>1200</height>
      <img_src>http://192.168.40.1/06.jpg</img_src>
      <m_width>592</m_width>
      <m_height>370</m_height>
      <pic_big_src>http://192.168.40.1/06.jpg</pic_big_src>
    </picture_list>
  </data>
</root>

 为了方便处理,想将这个xml的内容存入一个对象,通过观察分析xml的结构,写出相应的类:

在.h文件中:

#import <Foundation/Foundation.h>

@interface Status : NSObject
@property (strong,nonatomic) NSString *code;
@property (strong,nonatomic) NSString *msg;
@end

@interface Data : NSObject
@property (strong,nonatomic) NSString *picture_num;
@property (nonatomic) NSMutableArray *picture_list;
@end

@interface Picture_list : NSObject
@property (strong,nonatomic) NSString *picture_name;
@property (strong,nonatomic) NSString *picture_scope;
@property (strong,nonatomic) NSString *picture_sign;
@property (strong,nonatomic) NSString *is_cover;
@property (strong,nonatomic) NSString *width;
@property (strong,nonatomic) NSString *height;
@property (strong,nonatomic) NSString *img_src;
@property (strong,nonatomic) NSString *m_width;
@property (strong,nonatomic) NSString *m_height;
@property (strong,nonatomic) NSString *pic_big_src;
@end


@interface ashampBaiduImageList : NSObject
@property (strong,nonatomic) Status *status;
@property (strong,nonatomic) Data *data;
@end

  在.m文件中:

#import "ashampBaiduImageList.h"

@implementation ashampBaiduImageList
-(instancetype)init
{
    self=[super init];
    self.data=[[Data alloc] init];
    self.status=[[Status alloc] init];
    return self;
}
@end

@implementation Status
-(instancetype)init
{
    self=[super init];
    self.code=[[NSString alloc] init];
    self.msg=[[NSString alloc] init];
    return self;
}
@end

@implementation Data
-(instancetype)init
{
    self=[super init];
    self.picture_num=[[NSString alloc] init];
    self.picture_list=[[NSMutableArray alloc] init];
    return self;
}
@end

@implementation Picture_list
-(instancetype)init
{
    self=[super init];
    self.picture_name=[[NSString alloc] init];
    self.picture_scope=[[NSString alloc] init];
    self.picture_sign=[[NSString alloc] init];
    self.is_cover=[[NSString alloc] init];
    self.width=[[NSString alloc] init];
    self.height=[[NSString alloc] init];
    self.img_src=[[NSString alloc] init];
    self.m_width=[[NSString alloc] init];
    self.m_height=[[NSString alloc] init];
    self.pic_big_src=[[NSString alloc] init];
    return self;
}
@end

完成了针对xml的类的设计后,设计XMLConvert类

在.h文件中:

#import <Foundation/Foundation.h>
#import "ashampBaiduImageList.h"

@interface ashampXMLConvert : NSObject<NSXMLParserDelegate>

-(ashampBaiduImageList *)DeserializeXML:(NSString*)fromFile;

@end

在.m文件中:

#import "ashampXMLConvert.h"

@interface ashampXMLConvert ()
{
    //用于取xml节点的值
    NSString *value;
}
@end

@implementation ashampXMLConvert

ashampBaiduImageList *_imagelist;
Picture_list *pl;

-(ashampBaiduImageList *)DeserializeXML:(NSString*)fromFile
{
    _imagelist=[[ashampBaiduImageList alloc] init];
    pl=[[Picture_list alloc] init];
    NSArray *path= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filepath=[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@",path[0],fromFile]];
    NSData *data=[NSData dataWithContentsOfFile:filepath];
    NSXMLParser *parser=[[NSXMLParser alloc] initWithData:data];
    
    [parser setShouldProcessNamespaces:NO];
    
    [parser setShouldReportNamespacePrefixes:NO];
    
    [parser setShouldResolveExternalEntities:NO];

    [parser setDelegate:self];
    
    [parser parse];
    
    return _imagelist;
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    NSString *str=[[NSString alloc] initWithString:value];
    
    if ([elementName isEqualToString:@"code"]) {
        _imagelist.status.code=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"msg"]) {
        _imagelist.status.msg=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"picture_num"]) {
        _imagelist.data.picture_num=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"picture_list"]) {
        [_imagelist.data.picture_list addObject:pl];
    }
    if ([elementName isEqualToString:@"picture_name"]) {
        pl.picture_name=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"picture_scope"]) {
        pl.picture_scope=str;
    }
    if ([elementName isEqualToString:@"picture_sign"]) {
        pl.picture_sign=str;
    }
    if ([elementName isEqualToString:@"is_cover"]) {
        pl.is_cover=str;
    }
    if ([elementName isEqualToString:@"width"]) {
        pl.width=str;
    }
    if ([elementName isEqualToString:@"height"]) {
        pl.height=str;
    }
    if ([elementName isEqualToString:@"img_src"]) {
        pl.img_src=str;
        NSLog(@"%@ %@",elementName ,str);
    }
    if ([elementName isEqualToString:@"m_width"]) {
        pl.m_width=str;
    }
    if ([elementName isEqualToString:@"m_height"]) {
        pl.m_height=str;
    }
    if ([elementName isEqualToString:@"pic_big_src"]) {
        pl.pic_big_src=str;
    }
    
}

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

{
    
}

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

{
    value=string;
    //NSLog(@"Value:%@",string);
}

@end

使用方法:

    ashampXMLConvert *xc=[[ashampXMLConvert alloc] init];
    ashampBaiduImageList *bi= [xc DeserializeXML:@"x.xml"];

对象bi即为所需,其包含了xml内所有的节点内容:

原文地址:https://www.cnblogs.com/ashamp/p/3597381.html