ORVE WS (Dynamic) (6) Sicres3.0 format (2). Binding files
0. Introduction
There are some things I do not like from the previous class generation.- XML tags are not exactly copied to attribute names. Underscores are deleted.
- The class is too big
- There are static classes defined in the same file
1. Using binding files
In the last post, when generating java classes, Eclipse asked us for binding files. With binding files, we could vary the class generation process to get a more customized output.
2. Not omitting underscores in attributes.
In StackOverflow it is explained how to get underscores not deleted while generating classes from an xsd file. Here is a simple binding file for this purpose.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1"> <jaxb:globalBindings localScoping="toplevel"/> <jaxb:bindings schemaLocation="sicres3-0.xsd"> <!-- To include underscores in class and attribute names --> <!-- See https://stackoverflow.com/a/38494625/7704658 --> <jaxb:globalBindings underscoreBinding="asCharInWord"> </jaxb:bindings> </jaxb:bindings> |
3. Generating multiple class files instead of a single class
Blaise Doughan explains how to generate multiple classes.
The binding file ( sicres30jaxbinding.xml )that achieves this goal may be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.1"> <jaxb:globalBindings localScoping="toplevel"/> <jaxb:bindings schemaLocation="sicres30.xsd"> <!-- To include underscores in class and attribute names --> <!-- See https://stackoverflow.com/a/38494625/7704658 --> <!-- jaxb:globalBindings underscoreBinding="asCharInWord"/ --> <!-- Not Static inner classes --> <!-- See http://blog.bdoughan.com/2011/07/jaxb-xjc-and-nested-classes.html --> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Origen_o_Remitente']/xs:complexType"> <jaxb:class name="DeOrigenORemitente"/> </jaxb:bindings> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Destino']/xs:complexType"> <jaxb:class name="DeDestino"/> </jaxb:bindings> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Interesado']/xs:complexType"> <jaxb:class name="DeInteresado"/> </jaxb:bindings> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Asunto']/xs:complexType"> <jaxb:class name="DeAsunto"/> </jaxb:bindings> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Anexo']/xs:complexType"> <jaxb:class name="De_Anexo"/> </jaxb:bindings> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Internos_Control']/xs:complexType"> <jaxb:class name="DeInternosControl"/> </jaxb:bindings> <jaxb:bindings node="//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Formulario_Generico']/xs:complexType"> <jaxb:class name="DeFormularioGenerico"/> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings> |
In this case, the first part that deals with underscore treatment has been commented.
Line 6 represents the xsd file to be used "sicres30.xsd"
For each class that has been generated in the general file as a static class, a <jaxb:binding> tag has been used to specify that a separate class should be generated in another file.
For the field, "De_Origen_o_Remitente" the lines 13-15 are used and the explanation is:
To reference its node from the xsd file:
1 2 3 4 | <xs:element name="Fichero_Intercambio_SICRES_3"> <xs:complexType> <xs:sequence> <xs:element minOccurs="1" maxOccurs="1" name="De_Origen_o_Remitente"> |
and map it to the "DeOrigenORemitente" class, this node from the xsd file is referenced in the line
<jaxb:bindings node=
"//xs:element[@name='Fichero_Intercambio_SICRES_3']/xs:complexType/xs:sequence/xs:element[@name='De_Asunto']/xs:complexType">
The red elements try to reference the node from the xsd file.
Take into account that any small difference may cause a bad reference to the node!!!
To prevent locating the rest of the classes (DeDestino, DeInteresado, DeAsunto, De_Anexo, DeInternosControl and DeFormularioGenerioco) into the sale file, the inputs in the binding file should be provided (lines 17-39)
If now we right-click on the xsd file and select Generate - JAXB classes and provide the binding file
as in the image
we get these classes
And as we said in the previous post, it is adviseable to delete "ObjectFactory.java" and "jaxb.properties" files from the package to avoid unmarshalling errors!!!
Comments
Post a Comment