自定义XML

我们不但要知道如何解析XML,还要知道如何自定义XML 

下面给出一个简单的例子

#import "ViewController.h"
#import "DDXML.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //用来解析
    //DDXMLDocument
    
    
    //Users
    DDXMLElement *rootElement = [[DDXMLElement alloc] initWithName:@"Users"];
    
    for (int i = 0; i < 2; i++)
    {
        //User元素
        DDXMLElement *userElement = [[DDXMLElement alloc] initWithName:@"User"];
        
        //name
        DDXMLElement *nameElement = [[DDXMLElement alloc] initWithName:@"name" stringValue:[NSString stringWithFormat:@"张三%d",i+1]];
        //age
        DDXMLElement *ageElement = [[DDXMLElement alloc] initWithName:@"age" stringValue:[NSString stringWithFormat:@"%d",20+i]];
        
        //添加子元素
        [userElement addChild:nameElement];
        [userElement addChild:ageElement];
        
        //在nameElement中添加属性
        DDXMLNode *node = [DDXMLNode attributeWithName:@"id" stringValue:@"xxxx"];
        [nameElement addAttribute:node];
        
        
        
        
        //把User添加到根元素中
        [rootElement addChild:userElement];
        
        
    }
    
    
    NSLog(@"--%@",[rootElement description]);
    

}

//根据属性名字和值返回一个DDXMLNode对象
- (DDXMLNode *)nodeWithName:(NSString *)name value:(NSString *)value
{
    return [DDXMLNode attributeWithName:name stringValue:value];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值