I've run into a strange problem with JAXB. I've used xjc to generate my java classes from my XSD and all looks good. If I use schemagen, it produces a proper schema that matches my original xsd. However, if I use JAXBContext.generateSchema(), then the generated schema is incomplete.
I'm using Oracle Java 1.6.0_29 and jaxb-2.2.4-1.jar as the implementation. I'm enclosing the java code (which generates the schema), and the xsd below as well as the output of the jaxb call.
CalculateBorrowingDataResponse.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
version="1.1"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse"
xmlns:lssSt="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse"
xmlns:cbdRes="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- CalculateBorrowingData -->
<xsd:complexType name="CalculateBorrowingDataResponseType">
<xsd:sequence>
<xsd:element name="loanAgmt" type="cbdRes:LoanAgreementType" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="LoanAgreementType">
<xsd:sequence>
<xsd:element name="borrowingBasedPmtAmt" type="lssSt:borrowingBasedPmtAmt" minOccurs="0" maxOccurs="1" />
<xsd:element name="maxPmtAmt" type="lssSt:maxPmtAmt" minOccurs="0" maxOccurs="1" />
<xsd:element name="borrowingCapacityMin" type="lssSt:borrowingCapacityMin" minOccurs="0" maxOccurs="1" />
<xsd:element name="borrowingCapacityMax" type="lssSt:borrowingCapacityMax" minOccurs="0" maxOccurs="1" />
<xsd:element name="propertyValueMinAmt" type="lssSt:propertyValueMinAmt" minOccurs="0" maxOccurs="1" />
<xsd:element name="propertyValueMaxAmt" type="lssSt:propertyValueMaxAmt" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="calculateBorrowingDataResponse" type="cbdRes:CalculateBorrowingDataResponseType"/>
<xsd:simpleType name="borrowingBasedPmtAmt">
<xsd:restriction base="xsd:decimal" >
<xsd:totalDigits value="19" />
<xsd:fractionDigits value="4" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="maxPmtAmt">
<xsd:restriction base="xsd:decimal" >
<xsd:totalDigits value="19" />
<xsd:fractionDigits value="4" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="borrowingCapacityMin">
<xsd:restriction base="xsd:decimal" >
<xsd:totalDigits value="19" />
<xsd:fractionDigits value="4" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="borrowingCapacityMax">
<xsd:restriction base="xsd:decimal" >
<xsd:totalDigits value="19" />
<xsd:fractionDigits value="4" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="propertyValueMinAmt">
<xsd:restriction base="xsd:decimal" >
<xsd:totalDigits value="19" />
<xsd:fractionDigits value="4" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="propertyValueMaxAmt">
<xsd:restriction base="xsd:decimal" >
<xsd:totalDigits value="19" />
<xsd:fractionDigits value="4" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Java code:
// Creating the XML tree
JAXBContext jc = JAXBContext.newInstance( CalculateBorrowingDataResponseType.class );
Unmarshaller u = jc.createUnmarshaller();
// generate the schemas
final List<ByteArrayOutputStream> schemaStreams = new ArrayList<ByteArrayOutputStream>();
jc.generateSchema(new SchemaOutputResolver(){
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
schemaStreams.add(out);
StreamResult streamResult = new StreamResult(out);
streamResult.setSystemId("");
return streamResult;
}});
// convert to a list of string
List<String> schemas = new ArrayList<String>();
for( ByteArrayOutputStream os : schemaStreams )
{
schemas.add(os.toString());
System.out.println( os.toString());
}
Output of jaxbContext.generateSchema():
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" version="1.0" targetNamespace="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse" xmlns:tns="http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="CalculateBorrowingDataResponseType">
<xs:sequence>
<xs:element name="loanAgmt" type="tns:LoanAgreementType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LoanAgreementType">
<xs:sequence>
<xs:element name="borrowingBasedPmtAmt" type="xs:decimal" minOccurs="0"/>
<xs:element name="maxPmtAmt" type="xs:decimal" minOccurs="0"/>
<xs:element name="borrowingCapacityMin" type="xs:decimal" minOccurs="0"/>
<xs:element name="borrowingCapacityMax" type="xs:decimal" minOccurs="0"/>
<xs:element name="propertyValueMinAmt" type="xs:decimal" minOccurs="0"/>
<xs:element name="propertyValueMaxAmt" type="xs:decimal" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
As you can see, the output schema matches very closely, save and except that it is missing the element definition of calculateBorrowingDataResponse! If I use schemagen, however, the calculateBorrowingDataResponse element is generated.
Am I missing something in my SchemaOutputResolver setup or doing something incorrect/incomplete? Or is this a bug in the Jaxb RI?
Change
to
JAXB needs the information defined in the
package-info.java
file (generated byxjc
from your XSD) inside the above class' package.It doesn't creates the XSD element in question, because
CalculateBorrowingDataResponseType
doesn't have an@XmlRootElement
annotation.Why didn't
xjc
create one from the beginning? See the most authoritative explanation on the Internets regarding this matter.And why does your code generates the aforementioned element if you supply a package name to
JAXBContext.newInstance(...)
even thoughCalculateBorrowingDataResponseType
still missing the@XmlRootAnnotation
?I haven't got the faintest idea!Now I have!The following from the generated
ObjectFactory
class is what causes that element to be generated:In order to have the
ObjectFactory
class processed by the JAXBContext you either need to included in array of classes passed to create the JAXBContext:Or to create the JAXBContext on the package name of the generated classes:
Full Example
I get the following generated XML schema, that contains the
calculateBorrowingDataResponse
element:Why do you think that in roundrip 'schema1->XJC->schema2' schema1 and schema 2 should be identical? Do you have Java class generated for 'calculateBorrowingDataResponse' element by XJC? I don't think so - how would you expect it to be in generated schema then?