如何使用xml架构为xml反序列化生成java类?

前端之家收集整理的这篇文章主要介绍了如何使用xml架构为xml反序列化生成java类?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从一个模式生成 Java类的简单方法,以便我可以轻松地反序列化xml并使用对象进行解释.

使用Jaxb会很棒,但我对任何可以完成同样事情的框架持开放态度.架构是HL7 CDA架构.它非常复杂,我猜这就是我遇到问题的原因.

我尝试使用xjc和JAXB(这将是理想的)但我得到以下错误

xjc -d ~/code/ccd/java -p net.msdelta.cda -xmlschema -verbose CDA.xsd 
parsing a schema...
compiling a schema...
[INFO] generating code
unknown location
Exception in thread "main" java.lang.IllegalArgumentException: trying to create the same field twice: id
    at com.sun.codemodel.internal.JDefinedClass.field(JDefinedClass.java:408)
    at com.sun.codemodel.internal.JDefinedClass.field(JDefinedClass.java:379)
    at com.sun.tools.internal.xjc.generator.bean.field.AbstractFieldWithVar.createField(AbstractFieldWithVar.java:61)
    at com.sun.tools.internal.xjc.generator.bean.field.SingleField.<init>(SingleField.java:78)
    at com.sun.tools.internal.xjc.generator.bean.field.SingleField.<init>(SingleField.java:65)
    at sun.reflect.GeneratedConstructorAccessor8.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.sun.tools.internal.xjc.generator.bean.field.GenericFieldRenderer.generate(GenericFieldRenderer.java:53)
    at com.sun.tools.internal.xjc.generator.bean.field.DefaultFieldRenderer.generate(DefaultFieldRenderer.java:68)
    at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.generateFieldDecl(BeanGenerator.java:736)
    at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.generateClassBody(BeanGenerator.java:524)
    at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.<init>(BeanGenerator.java:224)
    at com.sun.tools.internal.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:164)
    at com.sun.tools.internal.xjc.model.Model.generateCode(Model.java:275)
    at com.sun.tools.internal.xjc.Driver.run(Driver.java:332)
    at com.sun.tools.internal.xjc.Driver.run(Driver.java:180)
    at com.sun.tools.internal.xjc.Driver._main(Driver.java:105)
    at com.sun.tools.internal.xjc.Driver.access$000(Driver.java:63)
    at com.sun.tools.internal.xjc.Driver$1.run(Driver.java:85)
如果此问题是由具有属性和具有相同名称的元素的复杂类型引起的,那么您可以使用JAXB模式绑定文件来更改与其中一个XML节点对应的字段名称
<jxb:bindings node="//xs:attributeGroup[@name='db.common.attributes']/xs:attribute[@name='version']">
            <jxb:property name="commonVersion"/>
        </jxb:bindings>

如果问题发生是因为元素在序列中多次出现(即在选择结构的内部和外部).您将需要使用以下XJC扩展:

<jxb:globalBindings>
    <xjc:simple />
</jxb:globalBindings>

有关完整示例,请参阅

> How do I create JAXB bindings for docbook

与JAXB和HL7 CDA架构相关的链接

> http://metro.1045641.n5.nabble.com/troubleshoot-quot-trying-to-create-the-same-field-twice-quot-error-td1059643.html

原文链接:https://www.f2er.com/xml/292421.html

猜你在找的XML相关文章