jaxb - xsd to java class for specified node - java

I have an XSD file with many ComplexType defined in it. I'm using XJC to generate java classes from the XSD file. It will generate classes for all the CompleXType node defined in the schema file. Is there any way I can use a filter in xjc command so that I can get classes of the specified ComplexType only, Instead of generating classes for the whole XSD file?

You can use binding files to somewhat achieve that:
Use <jaxb:schemaBindings map="false" ... /> to disable generation for the whole namespace/schema
Use <jaxb:class ref="com.acme.foo.Ignore"/> to map "unwanted" classes to some (existing) com.acme.foo.Ignore class.

Related

How to create service first soap webservice using spring-boot

We can see many tutorials that shows how to produce soap webservice using xsd in spring-boot. Is it possible to create soap webservice without xsd and from plain Java code using spring-boot-webservice module like we do using #webservice annotation in jax-ws
Guides like this start with an xsd file because they use xjc to create java classes from the XSD definition. xjc creates classes with JaxB annotations (javax.xml.bind.annotation). JaxB is an xml binding specification that has been part of the JDK since 1.6, and it requires that all types from the xsd exist as java classes.
I suggest that you do a tutorial that starts with an xsd an take a look at the auto generated classes. There is nothing to prevent you from writing you own classes instead of generating them from a wsdl, and if you don't refer to external schemas I prefer to do the code only approach (I hate xml configuration).
If you look at the tutorial, the gradle task "getJaxb" will create .java files into "build/generated-sources" compile them and copy them into "build/classes" if you copy the *.java files into "src/main/java" (keep the package structure) and delete delete/disable the "genJaxb" task in gradle, and delete your build folder, everything still works (it actually works better since you normally have red lines in your IDE because the XML beans don't exist until you run the generator the first time).
Now all you need to do is master the JaxB annotations, so you can write your own beans.

Create POJO class structure using JAXB from XML

I have looked at below mentioned question:
Java JAXB how to create POJO classes
Java JAXB xml pojo classes
Here is what I want to do.
I want to use JAXB and create POJO Java classes at runtime. If I change my XML in future, I don't need to make change POJO Java class structure manually. I need to only restart whole Java Application and it will generate all classes at runtime without user input
FYI :-
using XJC is not helpful as i can not merge it into application.
I have already automated whole business logic which is not affected by XML file.
Here are some maven modules which you may find helpful
jaxb2-maven-plugin
Mojo's JAXB-2 Maven plugin is used to create an object graph from XSDs based on the JAXB 2.1 implementation and to generate XSDs from JAXB annotated Java classes.
maven-jaxb2-plugin
This Maven plugin wraps and enhances the JAXB Schema Compiler (XJC) and allows compiling XML Schemas (as well as WSDL, DTDs, RELAX NG) into Java classes in Maven builds.
jaxb2-maven-plugin
This plugin uses the Java API for XML Binding (JAXB), version 2+, to generate Java classes from XML Schemas (and optionally binding files) and to create XML Schemas from annotated Java classes

Unsupported binding namespace while generating class files using xjc

I tried googling without any help. Apologies if there are any duplicates.
I have the following schema header for the file common.xsd
<xs:schema xmlns="http://www.vmware.com/vcloud/v1.5"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:meta="http://www.vmware.com/vcloud/meta"
jaxb:version="2.0"
jaxb:extensionBindingPrefixes="meta"
elementFormDefault="qualified"
targetNamespace="http://www.vmware.com/vcloud/v1.5"
version="1.0">
I am trying to generate class files using xjc command.
xjc -version
xjc version "JAXB 2.1.10 in JDK 6"
JavaTM Architecture for XML Binding(JAXB) Reference Implementation, (build JAXB 2.1.10 in JDK 6)
I am getting this error.
[info] [ERROR] Unsupported binding namespace "http://www.vmware.com/vcloud/meta". Perhaps you meant "http://java.sun.com/xml/ns/jaxb/xjc"?
[info] line 21 of file:/Users/kcherivirala/vmware/dev/corp/zephyr/services/networkservice/app/vcd-schema/src/main/xsd/vcloud/common.xsd
Any leads on this would be of great help.
The problem is here:
jaxb:extensionBindingPrefixes="meta"
jaxb:extensionBindingPrefixes declares prefixes of the vendor customization namespaces. See this link.
The JAXB RI provides additional customizations that are not defined by
the JAXB specification. Note the following:
These features may only be used when the JAXB XJC binding compiler is
run in the -extension mode.
All of the JAXB RI vendor extensions are defined in the
"http://java.sun.com/xml/ns/jaxb/xjc" namespace.
The namespaces containing extension binding declarations are specified
to a JAXB processor by the occurrence of the global attribute
#jaxb:extensionBindingPrefixes within an instance of
element. The value of this attribute is a whitespace-separated list of
namespace prefixes. For more information, please refer to section
6.1.1 of the JAXB Specification.
You only need this when you customize the binding. For instance you may use xjc:superClass to customize to extend a common super class. In this case, xjc would be in jaxb:extensionBindingPrefixes.
If you just compile your schema, the prefix of your schema will most definitely NOT be in jaxb:extensionBindingPrefixes. So, XJC just complained it was there but was not a binding extension.

XSD to Java custom datatype

I have xsd which contain the following: type="EAIschema:eCodes" where eCodes is another schema. When I compile it using xjc it returns:
"Cannot resolve the name 'EAIschema:eCodes' to a(n) 'type definition' component"
I want to know how to solve this problem
I'm not 100% sure about the error message, but it looks to me as if the JAXB classes for the other XSD were missing. If your XSD uses data structures of the other XSD then your JAXB classes will need those JAXB classes.
Solution: generate/ add the JAXB classes for the other XSD to your classpath.
If those classes are in a separate JAR, make sure it contains an episode file.
When the XJC tool is converting the XML schema(s) to Java classes it will automatically pull in imported/included schemas based on their system id. It those schemas are not available at the specified system id (or its not specified) then you could use an XML catalog.
For More Information
http://blog.bdoughan.com/2011/10/jaxb-xjc-imported-schemas-and-xml.html

JAXB, XJC -> create multiple class files

I'm using JAXB and XJC for first time.
I would like to generate Java classes from XML file so I use this online helper to generate schema from XML file.
After that I just use this command line to generate Java classes :
xjc myschema.xsd
it's work but I receive only one Java file and many static classes inside it. Is this possible to generate many java files that contain only one classe per file please ?
Thank you
By default JAXB (JSR-222) will create static inner classes for nested complex types to prevent class name conflicts. You can use an external binding file to disable this behaviour.
binding.xml
A binding file allows you to customize how Java classes are generated from an XML schema.
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
</jaxb:bindings>
XJC Call
The -b option is used with the XJC command to specify a binding file.
xjc -b binding.xml myschema.xsd
For More Information
http://blog.bdoughan.com/2011/07/jaxb-xjc-and-nested-classes.html

Categories