0

所以我有 YANG 模型文件。我想生成相应的 Java 类并能够将它们转换为 XML。现在,我尝试以下方法:

YANG -> Relax NG -> XSD -> Java with JAXB Annotations

YANG -> Relax NG可以通过pyang工具执行

放松 NG -> XSD可以通过trang工具执行

XSD -> Java with JAXB Annotations 可以通过xjc工具执行

不幸的是,结果看起来不太好,可能导致 XSD 缺少一些架构信息,或者我对工具的使用在某些方面是错误的。

例如: 杨:

grouping active-link-state {
       
        leaf remote-assigned-name {
            type rb-tg-types:node-assigned-name;
        }
        leaf actual-remote-sector-index {
            type rb-tg-types:radio-sector-index;
        }
        leaf actual-local-sector-index {
            type rb-tg-types:radio-sector-index;
        }
        leaf remote-mac-addr {
            type yang:mac-address;
        }
        leaf local-role {
            type link-role;
        }
        leaf link-uptime {
            type uint64;
            units "seconds";
        }
        uses active-link-health-state;
    }

XSD:

<xs:element name="active">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="remote-assigned-name"/>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="actual-remote-sector-index"/>
          <xs:element ref="actual-local-sector-index"/>
          <xs:element ref="remote-mac-addr"/>
          <xs:element ref="local-role"/>
          <xs:element ref="link-uptime"/>
          <xs:group ref="radio-bridge-tg-radio-common___active-link-health-state"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
</xs:element>

带有 JAXB 的 Java

protected String remoteAssignedName;

@XmlElementRefs({
    @XmlElementRef(name = "actual-remote-sector-index", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "actual-local-sector-index", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "remote-mac-addr", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "local-role", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "link-uptime", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "rssi", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "snr", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "mcs-rx", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "mcs-tx", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "tx-per", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "rx-per", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "tx-power-index", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "speed-rx", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "speed-tx", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "tx-beam", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "rx-beam", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "counters", type = JAXBElement.class, required = false)
})
protected List<JAXBElement<?>> actualRemoteSectorIndexOrActualLocalSectorIndexOrRemoteMacAddr;

如您所见,只有一个由一堆叶子生成的属性是不正确的

无论如何,这是实现这种转换的唯一方法吗?任何其他存在但我不知道的工具/方法?

谢谢

4

0 回答 0