jaxbcontext生成不完整的模式? [英] jaxbcontext generating incomplete schemas?
问题描述
我遇到了JAXB的一个奇怪问题。我用xjc从我的XSD生成我的java类,看起来很好看。如果我使用schemagen,它会生成一个与原始xsd匹配的正确模式。但是,如果我使用JAXBContext.generateSchema(),则生成的模式不完整。
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.
我正在使用Oracle Java 1.6.0_29和jaxb-2.2.4-1 .jar作为实现。我正在附上java代码(生成模式),以及下面的xsd以及jaxb调用的输出。
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:
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()的输出:
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>
如您所见,输出模式非常接近,保存并且除了它缺少元素calculateBorrowingDataResponse的定义!但是,如果我使用schemagen,则会生成
calculateBorrowingDataResponse元素。
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.
我在SchemaOutputResolver设置中遗漏了什么或者做了不正确/不完整的事情?或者这是Jaxb RI中的错误?
Am I missing something in my SchemaOutputResolver setup or doing something incorrect/incomplete? Or is this a bug in the Jaxb RI?
推荐答案
更改
JAXBContext.newInstance(CalculateBorrowingDataResponseType.class);
到
JAXBContext.newInstance(CalculateBorrowingDataResponseType.class.getPackage().getName());
JAXB需要 package-info.java中定义的信息
文件(由你的XSD中的 xjc
生成)在上面的类'包中。
JAXB needs the information defined in the package-info.java
file (generated by xjc
from your XSD) inside the above class' package.
它没有不会创建有问题的XSD元素,因为 CalculateBorrowingDataResponseType
没有 @XmlRootElement
注释。
It doesn't creates the XSD element in question, because CalculateBorrowingDataResponseType
doesn't have an @XmlRootElement
annotation.
为什么 xjc
从头开始创建一个?请参阅关于 Internets的最权威解释关于此事。
Why didn't xjc
create one from the beginning? See the most authoritative explanation on the Internets regarding this matter.
如果您为 JAXBContext.newInstance提供包名,为什么您的代码会生成上述元素? ...)
即使 CalculateBorrowingDataResponseType
仍然缺少 @XmlRootAnnotation
? 我没有最微弱的想法! 现在我有了!
And why does your code generates the aforementioned element if you supply a package name to JAXBContext.newInstance(...)
even though CalculateBorrowingDataResponseType
still missing the @XmlRootAnnotation
? I haven't got the faintest idea! Now I have!
这篇关于jaxbcontext生成不完整的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!