简体   繁体   English

Yang : 如何将 yang 模式转换为 XML

[英]Yang : How to convert yang schema to XML

I have a Yang file, i want to send data using yang schema in an xml format how do i do that.我有一个 Yang 文件,我想使用 yang 模式以 xml 格式发送数据,我该怎么做。

Suppose i have yang file like below假设我有如下 yang 文件

module jtest {
    namespace "jtest";
    prefix jtest;

    container jtest {
        container mycontainer1 {
            leaf myleaf1 {
                type string;
            }
        }

        container mycontainer2 {
            container innercontainer {
                leaf myleaf2 {
                    type string;
                }
            }
            leaf myleaf3 {
                type string;
            }
        }
        container mycontainer3 {
            leaf myleaf1 {
                type string;
            }
        }

    }
}

I want to send data in xml format as per yang schema, how do serialize or convert yang to xml.我想按照 yang 模式以 xml 格式发送数据,如何将 yang 序列化或转换为 xml。

Assuming (as per @predi comment) that you are probably asking about how to create an XML instance conforming to a YANG schema:假设(根据@predi 评论)您可能正在询问如何创建符合 YANG 模式的 XML 实例:

I'm not 100% sure if you mean programmatically (by code) or as an end-users, and if you use OpenDaylight (ODL) or not, but just in case, the DAEXIM project in ODL imports YANG from the ODL datastore to and from JSON, so you may be interested in that?我不是 100% 确定您是通过编程方式(通过代码)还是作为最终用户,以及您是否使用 OpenDaylight (ODL),但以防万一,ODL 中的 DAEXIM 项目从 ODL 数据存储中导入 YANG 到来自 JSON,所以您可能对此感兴趣? If you are an end-user, then perhaps the Data Export/Import User Guide is of use to you.如果您是最终用户,那么也许数据导出/导入用户指南对您有用。 If you are a developer, then have a look at the ExportTask class to learn how it writes JSON - and you should then be able to use ODL yangtools' XmlCodecFactory similarly to how DAEXIM is using the JSONCodecFactory to write XML instead of JSON.如果您是开发人员,请查看ExportTask类以了解它如何编写 JSON - 然后您应该能够使用 ODL yangtools 的XmlCodecFactory,类似于 DAEXIM 如何使用 JSONCodecFactory 编写 XML 而不是 JSON。

If you want to to tranform a YANG schema itself to XML, that's what YIN is for (but I don't think that's what you are asking).如果您想将 YANG 模式本身转换为 XML,这就是 YIN 的用途(但我认为这不是您要问的)。

You can use the pyang to generate xml file from yang file:您可以使用pyang从 yang 文件生成 xml 文件:

$ pyang -h
Usage: pyang [options] [<filename>...]
 
  -f FORMAT, --format=FORMAT
                        Convert to FORMAT.  Supported formats are: yang, yin,
                        dsdl, capability, depend, jsonxsl, jstree, jtox, name,
                        omni, sample-xml-skeleton, tree, uml
 
  Sample-xml-skeleton output specific options:
    --sample-xml-skeleton-doctype=DOCTYPE
                        Type of sample XML document (data or config).
    --sample-xml-skeleton-defaults
                        Insert leafs with defaults values.
    --sample-xml-skeleton-annotations
                        Add annotations as XML comments.
    --sample-xml-skeleton-path=SAMPLE_PATH
                        Subtree to print

like this:像这样:

pyang -f sample-xml-skeleton --sample-xml-skeleton-defaults -o output.xml input.yang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM