This is my general problem: I want everything to be automated as much as it can be.
This is my specific problem: I want to marshal/unmarshal CDATA. I know that I can specify my own adapter as here. For building process I use Maven and its maven-jaxb2-plugin. Is there some option how to annotate elements in .xsd so they will be automatically annotated by my custom adapter? I really don't want to manually change those after each build.
Any other ways how to solve my problem are more than welcome. :)
EDIT: I've ran to a subproblem and it is described there: JAXB binding - "unable to honor this conversion customization".
try this configuration
<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="yourXSD.xsd">
<bindings node="//xsd:complexType[#name='Certificate']//xsd:sequence//xsd:element[#name='certificate']">
<xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
</bindings>
<bindings node="//xsd:complexType[#name='User']//xsd:sequence//xsd:element[#name='certificate']">
<xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
</bindings>
</bindings>
</bindings>
i solved this issue on this link
this configuration work fine if is an xmltype like xs:string, xs:date, etc.
Try this configuration to solve your problem, using XmlJavaTypeAdapter.
Related
When generating Java classes from a XSD, how can I specify that for some specific node, a specific and already existent Java class should be used instead of trying to generate one?
Thank you very much.
You can use episode file to reference the existing classes. .episode files are just jaxb bindings file and has mappings between elements and java classes.
a) if those existing classes are also generated from (another) xsd. use below option to first create .episode file.
xjc -episode a.episode a.xsd
then use this a.episode that contains the mappings as input to the next xjc generation.
xjc b.xsd -extension -b a.episode
b) If you want to refer some random classes, then you may have to write your own episode file providing mapping between element and class reference like below.
sample.episode
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" if-exists="true" version="2.1">
<jaxb:bindings scd="x-schema::">
<jaxb:bindings scd="employee">
<jaxb:class ref="www1.example.Employee"/>
<jaxb:package name="www1.example" />
</jaxb:bindings>
</jaxb:bindings>
and use xjc b.xsd -extension -b sample.episode
You should use following binding customization
<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="../schema/yourSchema.xsd">
<bindings node="//xs:complexType[#name='Foo']">
<class ref="com.FooImpl"/>
</bindings>
</bindings>
</bindings>
I'm using gradle as build tool and configuring jaxb plugin jacobono. Have the below entry in build.gradle file:
jaxb {
bindingsDir = "src/main/resources/xjb"
xjc {
xsdDir = "src/main/resources/xsd"
generatePackage = "some.package"
}
}
under xjb directory, have binding.xml with the below content:
<?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:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xsi:schemaLocation="
http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<globalBindings>
<serializable uid="1" />
</globalBindings>
</bindings>
Although the files are getting generated, none of them implements serializable interface as i have defined in binding xml. I doubt on the binding dir configurations.
Any advice will be much helpful.
Moved the binding information to xsds, and it worked.
Not sure whether the plugin works properly with binging dir paramter as it didnt work even with absolute path.
I am using JAXB to process multiple XML documents.
My issue is that I cannot change the related xsd's and I don't want to amend the generated classes.
What I want to do is employ JAXB Binding customization files to achieve the desired result.
All I need to do is perform the equivalent of using the #XmlType.namespace annotation.
Is it possible to set the #XmlType.namespace annotation via JAXB Binding Customization files?
Disclaimer: I am the author of the jaxb2-annotate-plugin.
You can use the jaxb2-annotate-plugin to add arbitrary annotations to your schema-derived classes. #XmlType will be something like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
xmlns:jaxb="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"
jaxb:extensionBindingPrefixes="xjc annox"
version="2.1">
<jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[#name='someType']">
<annox:annotateClass>#javax.xml.bind.annotation.XmlType(namespace="urn:test")</annox:annotateClass>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
If you already have an #XmlType there, the customized annotation will be "merged" into it.
I have three XSD files: a.xsd, b.xsd and c.xsd
Each of them contains an xs:element called MyHeader
I try to use xjc to generate java classes from these three XSD file
Error when generating the java file
Here is the common part in all three XSDs:
<xs:element name="MyHeader">
<xs:complexType>
<xs:attribute name="Username" type="xs:string" />
<xs:attribute name="Password" type="xs:string" />
</xs:complexType>
</xs:element>
The error message is [xjc] [ERROR] 'MyHeader' is already defined
Then I try to use external bindings to solve the problem, because I really want MyHeader can be ONE java class.
My external binding is like
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" 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="./a.xsd" node="/xs:schema">
<bindings node="//xs:element[#name='MyHeader']">
<class name="MyHeader" />
</bindings>
</bindings>
<bindings schemaLocation="./b.xsd" node="/xs:schema">
<bindings node="//xs:element[#name='MyHeader']">
<class name="MyHeader" />
</bindings>
</bindings>
<bindings schemaLocation="./c.xsd" node="/xs:schema">
<bindings node="//xs:element[#name='MyHeader']">
<class name="MyHeader" />
</bindings>
</bindings>
</bindings>
Then it complains xpath error...
Can someone help me get rid of this issue. Thanks.
Your binding file needs the definition of the namespace prefix xs:. After adding this, a rename for MyHeader in one of the three xsd files works.
But you won't be able to rename three clashing element names in three different XML schema files.
One way around this would be to use different namespaces: then the elements wouldn't clash.
Removing the definition of this element from two schema files is another option.
Clearly, the entire schema design is flawed and should be reconsidered using an approach where common types or elements are defined in one file, with other files including this common base file.
I am not able to get a custom binding work for an XSD imported in a WSDL using wsdl:import tag.
I guess wsdl:type and xsd:import is the best way to import schema from an XSD but I am in need of a solution where I do not have to change the WSDL.
I am using CXF for generating the artifacts from the WSDL and it works fine when I do not use any custom binding but since my requirement is to change the name of the classes defined in the schema I intend to use a custom binding.
WSDL contains:
<definitions name="MMMWS" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xmmmd="http://xyz.com/abcdata"
xmlns:xmmms="http://xyz.com/abcservice"
targetNamespace="http://xyz.com/abcservice">
<import namespace="http://xyz.com/abcdata" location="abcdata.xsd"/>
.....
Binding file (.xjb) contains:
<jaxb:bindings version="2.0"
xmlns:jaxb="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"
jaxb:extensionBindingPrefixes="xjc"
schemaLocation="abcdata.xsd">
.....
The error I get is:
[ERROR] "file:abcdata.xsd" is not a part of this compilation. Is this a mistake for "file:abcdata.xjb"?
[ERROR] at line 7 column 51 of schema file:abcdata.xjb