自定义XML

前端之家收集整理的这篇文章主要介绍了自定义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
原文链接:https://www.f2er.com/xml/296067.html

猜你在找的XML相关文章