加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

自定义XML

发布时间:2020-12-16 08:54:08 所属栏目:百科 来源:网络整理
导读:我们不但要知道如何解析XML,还要知道如何自定义XML 下面给出一个简单的例子 #import "ViewController.h"#import "DDXML.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional

我们不但要知道如何解析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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读