使用pyang、json2xml和jtox将yang文件转换为xml时出现Python jtox错误

2024-10-05 11:33:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个yang文件(ietf interfaces.yang),通过在python中使用pyang、pybind、pyangbind模块,我生成了python类文件

C:\Python27\python C:\Python27\Scripts\pyang --plugindir C:\Python27\lib\site-packages\pyangbind/plugin -f pybind -o output.py C:\Users\amuqeed\Documents\MGSOFTNetconfBrowser\modules\ietf-interfaces@2014-05-08.yang 

我创建了一个包含字段name、description和enabled的字典,并将其存储在json文件中。 转换过程(https://pypi.org/project/json2xml/)讲述了如何使用pyang从yang文件生成jtox文件,然后使用json2xml库以及jtox和json文件作为输入来生成xml文件

Interface.json:

from output import ietf_interfaces
import pyangbind.lib.pybindJSON as pybindJSON

class InterfaceSet(object):

    def __init__(self):
        super(InterfaceSet, self).__init__()
        self.model = ietf_interfaces()
        new_interface = self.model.interfaces.interface.add("Lif1")
        new_interface.description = "NETCONF LIF"
        new_interface.enabled = "true"
        data = pybindJSON.dumps(new_interface)
        f = open("interface.json", "w")
        for line in data:
             f.write(line)
        f = open("interface.json", "r")
        data1 = f.readlines()
        print data1

Jtox文件生成:

C:\python27\python C:\python27\scripts\pyang-f jtox-o interface.jtox C:\Users\amuqeed\Documents\MGSOFTNetconfBrowser\modules\ietf-interfaces@2014-05-08.yang

interface.jtox:

{"tree": {"ietf-interfaces:interfaces": ["container", {"interface": ["list", {"enabled": ["leaf", "boolean"], "link-up-down-trap-enable": ["leaf", "enumeration"], "type": ["leaf", "identityref"], "name": ["leaf", "string"], "description": ["leaf", "string"]}, [["ietf-interfaces", "name"]]]}], "ietf-interfaces:interfaces-state": ["container", {"interface": ["list", {"speed": ["leaf", "uint64"], "statistics": ["container", {"out-octets": ["leaf", "uint64"], "discontinuity-time": ["leaf", "string"], "in-multicast-pkts": ["leaf", "uint64"], "out-unicast-pkts": ["leaf", "uint64"], "in-errors": ["leaf", "uint32"], "out-multicast-pkts": ["leaf", "uint64"], "in-discards": ["leaf", "uint32"], "in-unicast-pkts": ["leaf", "uint64"], "out-broadcast-pkts": ["leaf", "uint64"], "out-discards": ["leaf", "uint32"], "in-broadcast-pkts": ["leaf", "uint64"], "out-errors": ["leaf", "uint32"], "in-unknown-protos": ["leaf", "uint32"], "in-octets": ["leaf", "uint64"]}], "higher-layer-if": ["leaf-list", "string"], "name": ["leaf", "string"], "oper-status": ["leaf", "enumeration"], "phys-address": ["leaf", "string"], "lower-layer-if": ["leaf-list", "string"], "type": ["leaf", "identityref"], "last-change": ["leaf", "string"], "admin-status": ["leaf", "enumeration"], "if-index": ["leaf", "int32"]}, [["ietf-interfaces", "name"]]]}]}, "modules": {"ietf-inet-types": ["inet", "urn:ietf:params:xml:ns:yang:ietf-inet-types"], "ietf-yang-types": ["yang", "urn:ietf:params:xml:ns:yang:ietf-yang-types"], "ietf-interfaces": ["if", "urn:ietf:params:xml:ns:yang:ietf-interfaces"]}, "annotations": {}}

当我使用json2xml模块执行https://pypi.org/project/json2xml/中的以下步骤时

C:\Python27\python C:\Python27\Scripts\json2xml -t config -o interface.xml interface.jtox interface.json

我得到一个错误

json2xml:在/-无效节点处出错

有谁能帮助我理解json2xml处理转换的方式吗?我们需要jtox和json文件作为输入,因为我有一个项目需要从定义的xml文件生成xml文件。我有其他杨文件也为vrf,bgp和其他协议,需要转换


Tags: 文件injsonstringxmloutinterfacesinterface

热门问题