I'm using a standard xml schema, called isosts, I need to convert schema to java class using jaxb. In this xml schema, a lot of elements have attribute rid as type xs:IDREFS <xs:attribute name="rid" type="xs:IDREFS">. Jaxb converts xs:IDREFS to be list of Objects.
For my need, I want jaxb to make type xs:IDREFS to be just java string type in all the generated java class. And during marshall/unmarshall time, the value of attribute rid should all be handled as string. Since this is a standard schema, I have to customize jaxb. I'm not sure if this can be done in jaxb binding or adapter and how to do it. Can anyone help me?
This binding file (let's call it "binding.xjb) forces the type to String:
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<bindings schemaLocation="SomeSchemaName.xsd" node="/xs:schema">
<bindings node="//xs:attribute[#name='rid']">
<property>
<baseType>
<javaType name="java.lang.String"></javaType>
</baseType>
</property>
</bindings>
</bindings>
</bindings>
Compile the schema using
xjc -b binding.xjb SomeSchemaName.xsd
Related
There are a lot of answers related to this but for some reason, I am still confused and unable to get the Java objects for my XSD.
I am trying to create the Java objects from XSD using the XJC JAXB but when I run the XJC command it results in the error
[ERROR] Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
Hence, based on my research I found that I need to write the binding file so I wrote the binding file but still I am getting the same error which means that I am messing up something in the binding file. As I am unable to make it work even after a lot of try I thought of posting it and getting some solution.
Following is my XSD which is failing as per the XJC command failure message foo.xsd:
<xsd:complexType name="DocumentExtensionType">
<xsd:sequence>
<xsd:any namespace="##local" processContents="lax" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:anyAttribute processContents="lax"/>
</xsd:complexType>
Following are the JAXB bindings I tried and executed but still results in an error: foo.xjb
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
<jxb:bindings schemaLocation="foo.xsd">
<jxb:bindings
node="//xsd:complexType[#name='DocumentExtensionType']/xsd:sequence/xsd:choice/xsd:any[#namespace='http://checklists.nist.gov/sccf/0.1']">
<jxb:property name="any2" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
Following is another JAXB binding I've tried: foo.xjb
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="foo.xsd" version="1.0">
<schemaBindings>
<package name="com.track.doc"/>
</schemaBindings>
<bindings node="//xsd:complexType[#name='DocumentExtensionType']/xsd:sequence/xsd:any[#namespace='']">
<property name="any2"/>
</bindings>
</bindings>
</bindings>
Following is the command I am running in my terminal:
xjc -p "com.track.doc" -d "/Users/batman/Downloads/XSD" -b foo.xjb foo.xsd
Can someone please guide me what am I doing mistake?
After trying a lot of things finally this code worked for me. However, I posted only a chunk of XSD which was failing when I run this for this small chunk then it works but when I run for the whole XSD it still fails. Not completely sure what's going wrong.
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="foo.xsd" version="1.0">
<schemaBindings>
<package name="com.track.doc"/>
</schemaBindings>
<bindings node="//xsd:complexType[#name='DocumentExtensionType']/xsd:sequence">
<property name="seq1"/>
</bindings>
</bindings>
</bindings>
One more issue that I noticed in my XSD. If incase you are also having the similar issue then following might help:
Make sure that your XSD file which you are passing as input does not have any field with the name as value.
In my case I had a field with name = "value".
<xsd:attribute type="xsd:float" name="value" use="optional"/>
I replaced it with
<xsd:attribute type="xsd:float" name="floatValue" use="optional"/>
so I have written an external binding file for waterml2.xsd and so far have fixed the [Error] Property "Rows" is already defined and [Error] Property "Title" is already defined.
After fixing those I now am getting multiple issues dealing with collisions. For example:
[ERROR] A class/interface with the same name
"net.opengis.gml._3.SecondDefiningParameter" is already in use. Use a
class customization to resolve this conflict.
[Error] Two declarations cause a collision in the ObjectFactory class.
This is what the binding file looks like:
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<!-- In geometryPrimitives.xsd, fix so element and attribute both with name "rows" don't conflict -->
<bindings schemaLocation="http://schemas.opengis.net/gml/3.2.1/geometryPrimitives.xsd" version="3.2.1.2">
<bindings node="//xs:group/xs:sequence/xs:element[#name='rows']">
<property name="rowsElement"/>
</bindings>
</bindings>
<!-- In xlink.xsd, fix so element and attributes don't have the same "titles" conflict -->
<bindings schemaLocation="http://www.w3.org/1999/xlink.xsd" node="/xs:schema">
<schemaBindings>
<package name="net.opengis.wfs._2_1"/>
</schemaBindings>
</bindings>
<bindings schemaLocation="http://www.w3.org/1999/xlink.xsd" node="/xs:schema">
<bindings node="//xs:attributeGroup[#name='locatorAttrs']">
<bindings node=".//xs:attribute[#ref='xlink:title']">
<property name="LocatorAttrsTitle"/>
</bindings>
</bindings>
<bindings node="//xs:group[#name='locatorModel']">
<bindings node=".//xs:element[#ref='xlink:title']">
<property name="LocatorModelTitle"/>
</bindings>
</bindings>
<bindings node="//xs:attributeGroup[#name='arcAttrs']">
<bindings node=".//xs:attribute[#ref='xlink:title']">
<property name="ArcAttrsTitle"/>
</bindings>
</bindings>
<bindings node="//xs:group[#name='arcModel']">
<bindings node=".//xs:element[#ref='xlink:title']">
<property name="ArcModelTitle"/>
</bindings>
</bindings>
</bindings>
</bindings>
If anyone has any suggestions or has dealt with the WaterML 2.0 Schema before I would appreciate some help! Thanks.
The OGC schemas need a little additional binding information to correctly run through JAXB. You are a lot better off relying on the hard work at https://github.com/highsource/ogc-schemas.
To use the highsource bindings, it’s probably best to just clone the repository and use maven to build the bindings (there is good maven build support in this project). I.e.
git clone https://github.com/highsource/ogc-schemas.git
mvn install –D skipTests
I skipped the tests to speed things up and I had an issue with one test failing.
You could also just import the base pom.xml into Eclipse (or whatever IDE you use) and let the maven build go through.
Once it builds, you should end up with your bindings under the relevant folder. E.g. WaterML2.0:
ls waterml/2.0/target/generated-sources/xjc/net/opengis/waterml/v_2_0/
CategoricalTVPType.java
CategoricalTimeseriesType.java
CollectionPropertyType.java
CollectionType.java
CommentBlockPropertyType.java
and so on..
And there’s a XJB file in the target/classes folder.
One of the legacy applications XML schema definition has two types defined with same name in different cases.
<xsd:complexType name="effectType">
<xsd:complexType name="EFFECTType">
Schema validation works fine. But when I use JAXB to generate Java objects, it complains that two classes can't be generated with same.
As this schema used by existing clients, I can't change it. Is there any workaround for it so that it will allow me to generate Java objects for the schema using JAXB?
You could use below bindings to change the name of generated class.
<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:annox="http://annox.dev.java.net"
xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
<bindings schemaLocation="../path of your xsd/schema.xsd">
<bindings node="//xsd:schema//xsd:complexType[#name='EFFECTType']">
<class name="EffectTypeTwo" />
</bindings>
</bindings>
</bindings>
I have the following problem:
I have a maven project where I compile some XSD.
In one of the XSD i have a duplicate. the element entry (<xs:element name="VehicleRoot" minOccurs="0">) appears two times.
I would like to bind only one of the entry but i cannot figure out how to do it.
I tried to bind it (see below), but how can i bind only the fist one or the second one ?
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation="TravelItineraryReadLLS2.2.0RS.xsd" version="1.0">
<bindings node="//xs:element[#name='Vehicle']">
<class name="VehicleAttribute" />
</bindings>
</bindings>
</bindings>
any help would be appreciated !
If you can change the XSD, create a type for that element, and reference that type when you declare the 2 elements instead.
Consider the following type definition in my XSD:
<xs:complexType name="ED" mixed="true">
<xs:complexContent>
<xs:extension base="BIN">
<!-- I cut some data here -->
</xs:extension>
</xs:complexContent>
</xs:complexType>
I found that JAXB has a hard time generating code for mixed elements.
I tried using <jaxb:globalBindings generateMixedExtensions="true"/>, but that isn't supported very well and generates awkward List<Serializable> code.
So I thought I could modify some element through my custom binding:
<bindings
xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<bindings schemaLocation="../../processable/coreschemas/datatypes-base.xsd">
<bindings node="//xs:complexType[#mixed='true']" multiple="true">
<property>
<javaType><!-- What do I do here? --></javaType>
</property>
</bindings>
</bindings>
</bindings>
Basically, I want all elements which specify mixed=true to have a custom value or content field (String) which holds the CDATA between the tags. For instance for my ED type it could be like this in XML, the title element uses ED as its type:
<title>Hello, I'm a title!</title> should yield Hello, I'm a title! as its content.
How do I do this?
For those of you interested: I'm trying to generate code for the HL7v3 CDA specifications.