【发布时间】:2014-02-27 22:53:31
【问题描述】:
我在使用 JAXB 时遇到了一个奇怪的问题。我使用 xjc 从我的 XSD 生成我的 java 类,一切看起来都不错。如果我使用 schemagen,它会生成一个与我的原始 xsd 匹配的正确模式。但是,如果我使用 JAXBContext.generateSchema(),那么生成的架构是不完整的。
我使用 Oracle Java 1.6.0_29 和 jaxb-2.2.4-1.jar 作为实现。我附上了 java 代码(生成模式),下面的 xsd 以及 jaxb 调用的输出。
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 代码:
// 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());
}
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>
如您所见,输出模式非常匹配,除了缺少 calculateBorrowingDataResponse 的元素定义!但是,如果我使用 schemagen, 生成calculateBorrowingDataResponse 元素。
我是否在 SchemaOutputResolver 设置中遗漏了某些内容,或者做了一些不正确/不完整的事情?或者这是 Jaxb RI 中的错误?
【问题讨论】: