首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Yang模型生成xml/json

从Yang模型生成xml/json
EN

Stack Overflow用户
提问于 2016-06-22 05:14:40
回答 1查看 5.1K关注 0票数 5

我一直在尝试从java中的阳模型中生成样例xml/json数据,例如对于xsd,有一些工具可以生成样例xml。

我试过Pyang: 1.用Python写的。2.转换后给出的阴格式是与阳规范等价的xml格式。就像。如果我使用pyang将阳代码转换成阴代码,则如下所示:

代码语言:javascript
复制
 leaf templateSendPeriod {
      when "../exportProtocol!='netflow-v5'";
      type uint16;
      default 60;
      units seconds;
    }

这就是我得到的-

代码语言:javascript
复制
 <leaf name="templateSendPeriod">
      <when condition="../exportProtocol!='netflow-v5'"/>
      <type name="uint16"/>
      <default value="60"/>
      <units name="seconds"/>
    </leaf>

相反我想要的是

代码语言:javascript
复制
<templateSendPeriod></templateSendPeriod>

这样我就可以得到xml,输入细节,并在相同的阳上进行验证。

EN

回答 1

Stack Overflow用户

发布于 2018-12-03 18:48:56

您可以这样做,首先声明您的模型

代码语言:javascript
复制
// module name
module napalm-star-wars {

    // boilerplate
    yang-version "1";
    namespace "https://napalm-yang.readthedocs.io/napalm-star-wars";

    prefix "napalm-star-wars";

    // identity to unequivocally identify the faction an individual belongs to
    identity AFFILIATION {
      description "To which group someone belongs to";
    }

    identity EMPIRE {
      base AFFILIATION;
      description "Affiliated to the empire";
    }

    identity REBEL_ALLIANCE {
      base AFFILIATION;
      description "Affiliated to the rebel alliance";
    }

    // new type to enforce correctness of the data
    typedef age {
      type uint16 {
        range 1..2000;
      }
    }

    // this grouping will all the personal data we will assign to individuals
    grouping personal-data {
        leaf name {
            type string;
        }
        leaf age {
            type age;
        }
        leaf affiliation {
            type identityref {
                base napalm-star-wars:AFFILIATION;
            }
        }
    }

    // this is the root object defined by the model
    container universe {
        list individual {
            // identify each individual by using the name as key
            key "name";

            // each individual will have the elements defined in the grouping
            uses personal-data;
        }
    }
}

模型的树表示吗?

代码语言:javascript
复制
$ pyang -f tree napalm-star-wars.yang
module: napalm-star-wars
    +--rw roster
        +--rw individual* [name]
           +--rw name           string
           +--rw age?           age
           +--rw affiliation?   identityref

稍后,在您的python代码中使用此代码:

代码语言:javascript
复制
>>> import napalm_star_wars
>>>
>>> sw = napalm_star_wars.napalm_star_wars()
>>>
>>> obi = sw.universe.individual.add("Obi-Wan Kenobi")
>>> obi.affiliation = "REBEL_ALLIANCE"
>>> obi.age = 57
>>>
>>> luke = sw.universe.individual.add("Luke Skywalker")
>>> luke.affiliation = "REBEL_ALLIANCE"
>>> luke.age = 19 

在这里,您可以根据您的选择获得json或xml的答案。

代码语言:javascript
复制
import json
>>> print(json.dumps(sw.get(), indent=4))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37959059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档