[英]Generate xml/json from Yang model
I've been trying to find something that can generate sample xml/json data from the yang model in java , For eg for xsd there are tools where you can generate the Sample xml. 我一直在努力寻找可以从java中的yang模型生成样本xml / json数据的东西。例如,对于xsd,有一些工具可以生成Sample xml。
I tried Pyang: 1. Its in Python. 我试过Pyang:1。用Python编写。 2. After conversion it gives me yin format which is xml equivalent of yang specs. 转换后,它给出了yin格式,它与xang规格相当。 for Eg. 对于Eg。 for following yang code if I convert it to YIN using pyang: 如果我使用pyang将其转换为YIN,请使用yang代码:
leaf templateSendPeriod {
when "../exportProtocol!='netflow-v5'";
type uint16;
default 60;
units seconds;
}
This is what I got- 这就是我得到的 -
<leaf name="templateSendPeriod">
<when condition="../exportProtocol!='netflow-v5'"/>
<type name="uint16"/>
<default value="60"/>
<units name="seconds"/>
</leaf>
Instead what I wanted was 相反,我想要的是
<templateSendPeriod></templateSendPeriod>
So that I could get the xml, enter the details and validate against the same yang. 这样我就可以获得xml,输入详细信息并验证相同的阳。
You can do this way, First declare your model 你可以这样做,首先声明你的模型
// 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;
}
}
}
Do tree representation of your model 做树模型的代表
$ 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
later use this in your python code: 稍后在你的python代码中使用它:
>>> 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
Here you get the answer for getting json or xml as per your choice.. 在这里,您可以根据自己的选择获得json或xml的答案。
import json
>>> print(json.dumps(sw.get(), indent=4))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.