How can I generate java classes to deserialize xml using xml schema?

I need a simple way to generate Java classes from a schema so that I can easily deserialize xml and interpret using objects.

Using Jaxb would be great, but I'm open to any frameworks that will do the exact same thing. The circuit is a CDA HL7 circuit. It is very difficult, and I guess why I have problems with it.

I tried using xjc and JAXB (that would be perfect), but I get the following error

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) 
+6
source share
4 answers

If this problem is caused by a complex type that has both an attribute and an element with the same name, you can use the JAXB schema bindings file to change the name of the field corresponding to one of the XML nodes:

  <jxb:bindings node="//xs:attributeGroup[@name='db.common.attributes']/xs:attribute[@name='version']"> <jxb:property name="commonVersion"/> </jxb:bindings> 

If the problem arises because the element appears several times in the sequence (i.e. both inside and outside the selection structure). You will need to use the following XJC extension:

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

For a complete example, see

JAXB and HL7 CDA Links

+6
source

I think you may have clicked http://java.net/jira/browse/JAXB-512

JiBX (http://jibx.sourceforge.net/) is another alternative that is better in terms of performance than JAXB. This requires the creation of a configuration file, but there are tools that can automatically generate them for you. Read more here β†’ http://jibx.sourceforge.net/fromschema/index.html

0
source

Eclipse EMF can generate classes from your xsd schema, with support for (de) serialization. EMF can also be used in stand-alone (non-OSGi) applications.

0
source

Exception Information: java.lang.IllegalArgumentException: attempt to create the same field twice.

As the exception says, you are trying to generate code (JAXB) for a schema with the same name for multiple attributes and elements. The easiest way is to add a JAXB-Binding file.

JAXB binding purpose: This file is used to specify alias names for duplicate elements / attributes, i.e. if the name "id" is repeated, you can specify an alias as "id1" with the path of this element / attribute.

Sample Binding File:

 <jxb:globalBindings> <xjc:simple /> </jxb:globalBindings> <jxb:bindings schemaLocation="Sample.xsd"> <!-- ATTRIBUTES --> <jxb:bindings node="//xs:element[@name='sample']/xs:complexType/xs:attribute[@name='id']"> <jxb:property name="id1"/> </jxb:bindings> <jxb:bindings node="//xs:element[@name='innersample']/xs:complexType/xs:attribute[@name='id']"> <jxb:property name="id2"/> </jxb:bindings> <!-- ELEMENTS --> <jxb:bindings node="//xs:element[@name='sample']/xs:complexType/xs:sequence/xs:element[@name='ID']"> <jxb:property name="id3"/> </jxb:bindings> <jxb:bindings node="//xs:element[@name='innersample']/xs:complexType/xs:sequence/xs:element[@name='ID']"> <jxb:property name="id4"/> </jxb:bindings> </jxb:bindings> 

If you use the NETBEANS IDE to bind JAXB, add the bind file at creation time and check the "Extension" box when using XJC.

0
source

Source: https://habr.com/ru/post/896178/


All Articles