Jin
Jin

Reputation: 21

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.

Suppose i have yang file like below

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.

Upvotes: 2

Views: 5139

Answers (2)

Pengwei Chen
Pengwei Chen

Reputation: 97

You can use the pyang to generate xml file from yang file:

$ 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

Upvotes: 2

vorburger
vorburger

Reputation: 3928

Assuming (as per @predi comment) that you are probably asking about how to create an XML instance conforming to a YANG schema:

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? 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.

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).

Upvotes: 1

Related Questions