JAXB Overwriting package-info.java: What should be "namespace"? - java

We're using xjc to generate JAXB Java classes for XML generation. Everything works fine except we tried to adjust the generated namespace prefixes as described here. We're stuck with "solution 2", adjusting package-info.java, due to the JAXB version we're using.
The structure we have is several imports deep: root namespace imports other namespace, which in turn imports yet a third one.
MCVE xsds
root.xsd (imports other.xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="root" xmlns:other="other" targetNamespace="root" version="1.0">
<xs:import namespace="other" schemaLocation="other.xsd" />
<xs:element name="rootElem">
<xs:complexType>
<xs:choice>
<xs:element ref="rootElem1"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="rootElem1" nillable="false">
<xs:complexType>
<xs:sequence>
<xs:element name="data">
<xs:complexType>
<xs:choice>
<xs:element ref="other:otherElem"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
other.xsd (imports third.xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:other="other" xmlns:third="third" targetNamespace="other" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
<xsd:import namespace="third" schemaLocation="third.xsd" />
<xsd:element name="otherElem">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="third:thirdElem" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
third.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:third="third" targetNamespace="third" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.0">
<xsd:element name="thirdElem">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="thirdData" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
With this
Simple test case
#Test
public void test() throws JAXBException
{
Marshaller marshaller = JAXBContext.newInstance("test.jaxb.generated").createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");
RootElem root = new RootElem();
RootElem1 root1 = new RootElem1();
Data d = new Data();
OtherElem other = new OtherElem();
ThirdElem thirdElem = new ThirdElem();
thirdElem.setThirdData("third");
other.setThirdElem(thirdElem);
d.setOtherElem(other);
root1.setData(d);
root.setRootElem1(root1);
Path path = Paths.get("target", "outfile.xml");
Result result = new StreamResult(path.toFile());
marshaller.marshal(root, result);
}
this results in this
Generated XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:rootElem xmlns:ns2="other" xmlns:ns3="third" xmlns:ns4="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema.xsd">
<ns4:rootElem1>
<data>
<ns2:otherElem>
<ns3:thirdElem>
<ns3:thirdData>third</ns3:thirdData>
</ns3:thirdElem>
</ns2:otherElem>
</data>
</ns4:rootElem1>
</ns4:rootElem>
and everything is fine (except data which doesn't have any namespace associated, I'm assuming because that's an inner type).
Now this is partly the question: here's the
generated package-info.java
#javax.xml.bind.annotation.XmlSchema(namespace = "other", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package test.jaxb.generated;
Why is the namespace given referring to other? My root namespace is root. (root.xsd is the only file we're giving our Maven jaxb2-maven-plugin; we can include the others, it makes no difference).
Wrong replacement package-info.java
If we overwrite the generated one with this:
#javax.xml.bind.annotation.XmlSchema(
namespace = "root",
xmlns = {
#javax.xml.bind.annotation.XmlNs(prefix = "t", namespaceURI = "third"),
#javax.xml.bind.annotation.XmlNs(prefix = "o", namespaceURI = "other"),
#javax.xml.bind.annotation.XmlNs(prefix = "r", namespaceURI = "root")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package test.jaxb.generated;
which we initially did because we assumed we have to give the root namespace here - this is the
Wrong Generated XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<r:rootElem xmlns:t="third" xmlns:o="other" xmlns:r="root" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/schema.xsd">
<r:rootElem1>
<data>
<r:otherElem>
<t:thirdElem>
<t:thirdData>third</t:thirdData>
</t:thirdElem>
</r:otherElem>
</data>
</r:rootElem1>
</r:rootElem>
Now the namespaces are pretty, but wrong! otherElem belongs to o, not r.
Changing the namespace in the overwriting file back to other fixes the error, but again:
the question is why is the required namespace other here? Just as confusing is the fact that the third imported layer is correct either way.
The problem is fixed, but we'd like to understand the concept. What are we missing?
EDIT:
For completeness' sake, here's the build section of my pom.xml:
<build>
<plugins>
<plugin>
<!-- run xjc -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<id>xjc-generate_classes</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>test.jaxb.generated</packageName>
<schemaFiles>
root.xsd
</schemaFiles>
<schemaDirectory>${basedir}/src/main/resources/schemata</schemaDirectory>
<extension>true</extension>
<bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory>
<outputDirectory>${basedir}/target/generated-sources/jaxb/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- overwrite created package-info.java -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/generated-sources/jaxb/test/jaxb/generated</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources/bindings</directory>
<include>package-info.java</include>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

The problem is that you generate all your classes in one package (configured using the packageName of the Maven plugin.
Do not do this.
JAXB is somewhat based on the concept of package-namespace correspondence. There should be one package per namespace you use. While it is technically possible to do it otherwise, you'll be facing one problem after another. So it is better to follow this concept and use or generate one package per namespace. You can still configure target packages - but using the binding files instead of plugin configuration elements.

Related

jaxb2-maven-plugin generating package-info.java with xmlns prefixes

I want to generate java classes with the jaxb2-maven-plugin. I am using the following configuration:
pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>SomeID</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<extension>true</extension>
<clearOutputDir>true</clearOutputDir>
<sources>
<source>src/main/xsd/schema.xsd</source>
</sources>
<noGeneratedHeaderComments>true</noGeneratedHeaderComments>
</configuration>
</execution>
</executions>
</plugin>
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://my.target.namespace/uri"
xmlns="http://my.target.namespace/uri"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:h="http://my.uri.for.prefix.h"
xmlns:f="http://my.target.namespace/uri">
<xsd:import namespace="http://my.uri.for.prefix.h" schemaLocation="schema2.xsd"/>
<xsd:complexType name="FooType">
<xsd:sequence>
<xsd:element ref="h:something" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="FooType" type="FooType" />
</xsd:schema>
The Jaxb2 plugin is generating me the following package-info.java:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;
But, what I want to get is this:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", xmlns = {
#XmlNs(prefix="f", namespaceURI="http://my.target.namespace/uri"),
#XmlNs(prefix="h", namespaceURI="http://my.uri.for.prefix.h")
}, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;
import javax.xml.bind.annotation.XmlNs;
The prefixes are missing in my generated file. How to do that? I tried already to create a binding file but this didn't worked how I expected.
Please see this answer on how to solve this problem:
https://stackoverflow.com/a/10812236/1389219
The answer is very well written and easy to follow. Basically you will have to:
Drop jaxb2-maven-plugin in favour of maven-jaxb2-plugin.
Include the jaxb2-namespace-prefix dependency and provide the <arg>-Xnamespace-prefix</arg>.
Write a new bindings.xml file which is only a few lines long.
Your POM file will become more verbose, but it is worth it to have a package-info.java generated the way you require.
As a bonus, there are a heap of additional plugins and dependencies related to maven-jaxb2-plugin that provide extra features. One that I found helpful was jaxb2-rich-contract-plugin that gave the ability to generate builders and make the generated classes immutable*.
* Well, not strictly speaking immutable (as it just changes the setter methods to be package private), but enough to make them feel safer.

I would like to add #XmlRoot annotation on CXF codegen

I have a WSDL file which contains the following entry:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="urn:CP_Ablakido" xmlns:s0="urn:CP_Ablakido" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema elementFormDefault="qualified" targetNamespace="urn:CP_Ablakido">
<xsd:element name="GetList_11" type="s0:InputMapping1"/>
<xsd:complexType name="InputMapping1">
<xsd:sequence>
<xsd:element name="Qualification" type="xsd:string"/>
<xsd:element name="startRecord" type="xsd:string"/>
<xsd:element name="maxLimit" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="GetList_11Response" type="s0:OutputMapping1"/>
<xsd:complexType name="OutputMapping1">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="getListValues">
<xsd:complexType>....
I use the CXF Codegen plugin with the following settings:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/target/generated-sources/wsdl2java</sourceRoot>
<encoding>UTF-8</encoding>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
And the generated java code look like:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "OutputMapping1", propOrder = {
"getListValues"
})
public class OutputMapping1 {
#XmlElement(required = true)
protected List<OutputMapping1 .GetListValues> getListValues;
The problem is that the #XmlRootElement is missing from here. There was another similar questions like
maven-cxf-codegen-plugin using Jaxb binding to add inheritance for all generated classes
Annotating CXF (wsdl2java) generated package
externally create jaxb annotations for class
As the other answers mentioned that I can put bindings file.
So I created a binding file with the following content:
<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"
version="2.0">
<jaxb:bindings node="//xsd:element[#name='GetList_11Response']">
<annox:annotate target="class">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="GetList_11Response"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
And I've added the following block to POM.XML:
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.wsdl</wsdl>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.xjb
</bindingFile>
</bindingFiles>
</wsdlOption>
</wsdlOptions>
After that I've got error message:
com.sun.istack.SAXParseException2; systemId: file:/Project/icp-integration/icpiCameI/src/main/resources/wsdl/CP_Ablakido_1.xjb; lineNumber: 9; columnNumber: 72; XPath evaluation of "//xs:element[#name='GetList_11Response']" results in empty target node
at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError(Internalizer.java:624)
at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError(Internalizer.java:618)
at com.sun.tools.xjc.reader.internalizer.Internalizer.buildTargetNodeMap(Internalizer.java:294)
at com.sun.tools.xjc.reader.internalizer.Internalizer.buildTargetNodeMap(Internalizer.java:390)
at com.sun.tools.xjc.reader.internalizer.Internalizer.transform(Internalizer.java:146)
So I don't know how exactly can I describe that when the complexType is OutputMapping1 then put #XmlRootElement in OutputMapping1.java with the name of "GetList_11Response".
I found the solution. It was trickey, because:
Have to handle that the XSD is inlined in WSDL. (The trick is schemaLocation="CP_Ablakido_1.wsdl#types1". It tells the JAXB that use node in WSDL file.)
Have to configure CXF to use XJC plugin (annox).
Have to add the following fragments to POM.XML:
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.wsdl</wsdl>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.xjb</bindingFile>
</bindingFiles>
<extraargs><extraarg>-xjc-Xannotate</extraarg></extraargs>
</wsdlOption>
and dependencies have to add to plugin:
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf.xjcplugins</groupId>
<artifactId>cxf-xjc-ts</artifactId>
<version>3.0.5</version>
</dependency>
The XJB file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:extensionBindingPrefixes="annox xjc"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:annox="http://annox.dev.java.net"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance">
<jaxb:bindings schemaLocation="CP_Ablakido_1.wsdl#types1" node="/xs:schema">
<jaxb:bindings node="//xs:complexType[#name='InputMapping1']">
<annox:annotate target="class">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="GetList_11"/>
</annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="//xs:complexType[#name='OutputMapping1']">
<annox:annotate target="class">
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="GetList_11Response"/>
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
#du-it As I understand, you don't want to apply XmlRootElement to all of the classes automatically.
This is the reason:
https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always
Therefore, doing it for each type separately is the proper way to do it.

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name?

I am getting below error while maven build on Linux server though on windows
machine it works fine
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'customer:CustomerApplication' to a(n) 'element declaration' component.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:2537)
Here is relevant snippet from pom.xml
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<configuration>
<schemaDirectory>src/main/resources/META-INF/schema</schemaDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
First XSD relevant part
<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cohbe.org/CustomerRequest"
xmlns:customer="http://www.cohbe.org/customer"
targetNamespace="http://www.cohbe.org/CustomerRequest"
elementFormDefault="qualified">
<xsd:import schemaLocation="CustomerDetails.xsd"
namespace="http://www.cohbe.org/customer"/>
<xsd:element name="CustomerNewRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="customer:CustomerApplicationDetail" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
</xsd:schema>
CustomerDetails.xsd location is same as of First XSD. Here is relevant part
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema version="2.15"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cohbe.org/CustDetails"
targetNamespace="http://www.cohbe.org/CustDetails"
xmlns:countries="http://www.cohbe.org/Counties"
elementFormDefault="qualified">
<!-- Version 2.15 -->
<xsd:import namespace="http://www.cohbe.org/states" schemaLocation="States.xsd"/>
<xsd:element name="CustomerApplicationDetail"
type="CustomerApplicationDetail"/>
<xsd:complexType name="CustomerApplicationDetail">
.....
</xsd:schema>
UPDATE:- second file attributes are :-
xmlns="http://www.cohbe.org/customer"
targetNamespace="http://www.cohbe.org/customer"
instead of
xmlns="http://www.cohbe.org/CustDetails"
targetNamespace="http://www.cohbe.org/CustDetails"
It was copy/paste error in this question So I am getting same error i.e org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name

Can't use jaxb bindings on individual xsd elements

I'm using jaxb to generate java source code from an xsd file.
I want to be able to specify which packages the sources are generated in on a per element basis, however whenever I generate sources I get the following error:
[ERROR] ****/src/main/xjb/common.xjb[8,24]
com.sun.istack.SAXParseException2: compiler was unable to honor this schemaBinding customization. It is attached to a wrong place, or its inconsistent with other bindings.
My bindings file common.xjb is attempting to place the element with the name (attribute value) 'api' in the package 'com.myxml.common.api':
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="../xsd/common/common.xsd" node="/xs:schema">
<jxb:bindings node="//xs:element[#name='api']">
<jxb:schemaBindings>
<jxb:package name="com.myxml.common.api" />
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
My xsd file common.xsd is:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1" xml:lang="en">
<xs:element name='api'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string" />
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
And I'm using the following Maven plugin to get everything going:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>schema00-generate</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<schemaFiles>common/common.xsd</schemaFiles>
<bindingFiles>common.xjb</bindingFiles>
<bindingDirectory>${project.basedir}/src/main/xjb</bindingDirectory>
</configuration>
</execution>
</executions>
</plugin>
Why am I getting this error and how can I resolve it? I don't have any other bindings in use at this stage that I'm aware of.
Elements from the same namespace cannot be mapped to different packages so the package cannot be defined for anything other than the top level

How generate XMLElementWrapper annotation with xjc and customized binding

I'm using JAXB and xjc to compile my XML Schema into Java classes. I do not want to manually edit this generated classes. I have xml schema like that:
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element ref="item" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
The xjc generates a class Items that only contains a list of Item objects. Is there any chance to omit the class Items and have a list of Item objects directly in the Rootclass?
I know that this can be done with #XMLElementWrapper annotation, but I don't know how to tell xjc to create such.
Thanks for any suggestions!
Best regards,
Markus
Bjarne Hansen developed a plugin for xjc that was able to take care of this. Unfortunately, the link to the original implementation is now dead. However, there is a project by Dmitry Katsubo on github, based on Bjarne's original code with some additional improvements.
→ https://github.com/dmak/jaxb-xew-plugin
(Just for reference: the original link, now dead: http://www.conspicio.dk/blog/bjarne/jaxb-xmlelementwrapper-plugin)
First lets break up your schema so that there are no inner classes generated:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="root" type="Root" />
<xs:complexType name="Root">
<xs:sequence>
<xs:element name="items" type="Items" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Items">
<xs:sequence>
<xs:element name="item" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
You'll still get extra classes, just not all in one file. Now you want to add a section to your build to use the jaxb-xew-plugin. I use Maven, so for me this looks like:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<args>
<arg>-no-header</arg>
<arg>-Xxew</arg>
<arg>-Xxew:instantiate lazy</arg>
<arg>-Xxew:delete</arg>
</args>
<plugins>
<plugin>
<groupId>com.github.jaxb-xew-plugin</groupId>
<artifactId>jaxb-xew-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</configuration>
</execution>
</executions>
</plugin>
If you start using namespaces so that your generated classes have package names, leave off the -Xxew:delete flag, as there's a bug that I recently fixed where it was deleting objects it shouldn't. Alternatively, you could grab the code from github and use it as 1.1-SNAPSHOT.
When I do that I get the code generated that I think you're looking for:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "Root", propOrder = {
"items"
})
public class Root {
#XmlElementWrapper(name = "items", required = true)
#XmlElement(name = "item")
protected List<String> items;
public List<String> getItems() {
if (items == null) {
items = new ArrayList<String>();
}
return items;
}
public void setItems(List<String> items) {
this.items = items;
}
}

Categories

Resources