Java 从Yang模型生成xml/json
我一直在努力寻找一些可以从java中的yang模型生成示例xml/json数据的工具,例如对于xsd,有一些工具可以生成示例xml 我试过平: 1.它是用Python编写的。 2.转换后,它给了我阴格式,这是相当于阳规范的xml。 例如,对于以下阳代码,如果我使用pyang将其转换为阴:Java 从Yang模型生成xml/json,java,json,xml,ietf-netmod-yang,Java,Json,Xml,Ietf Netmod Yang,我一直在努力寻找一些可以从java中的yang模型生成示例xml/json数据的工具,例如对于xsd,有一些工具可以生成示例xml 我试过平: 1.它是用Python编写的。 2.转换后,它给了我阴格式,这是相当于阳规范的xml。 例如,对于以下阳代码,如果我使用pyang将其转换为阴: leaf templateSendPeriod { when "../exportProtocol!='netflow-v5'"; type uint16; default
leaf templateSendPeriod {
when "../exportProtocol!='netflow-v5'";
type uint16;
default 60;
units seconds;
}
这就是我得到的-
<leaf name="templateSendPeriod">
<when condition="../exportProtocol!='netflow-v5'"/>
<type name="uint16"/>
<default value="60"/>
<units name="seconds"/>
</leaf>
相反,我想要的是
<templateSendPeriod></templateSendPeriod>
这样我就可以获取xml,输入详细信息并根据相同的数据进行验证。您可以这样做,
首先声明您的模型
// 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;
}
}
}
是否对模型进行树表示
$ 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代码中使用:
>>> 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
import json
>>> print(json.dumps(sw.get(), indent=4))
Pyang可以为您生成xml框架。使用以下命令:
pyang-f示例xml骨架文件-o输出文件
。