我遇到了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中的一个错误?
答案 0 :(得分:6)
更改
JAXBContext.newInstance(CalculateBorrowingDataResponseType.class);
到
JAXBContext.newInstance(CalculateBorrowingDataResponseType.class.getPackage().getName());
JAXB需要在package-info.java
文件中定义的信息(由您的XSD中的xjc
生成)在上面的类#39;封装
它没有创建有问题的XSD元素,因为CalculateBorrowingDataResponseType
没有@XmlRootElement
注释。
为什么没有xjc
从一开始创建一个?请参阅the most authoritative explanation on the Internets regarding this matter。
如果您向[{1}}提供包名称,即使JAXBContext.newInstance(...)
仍然缺少CalculateBorrowingDataResponseType
,您的代码为什么会生成上述元素? 我没有得到最微弱的想法! Now I have!
答案 1 :(得分:5)
生成的ObjectFactory
类中的以下内容是导致生成该元素的原因:
@XmlElementDecl(namespace = "http://www.domain.com/ClientServices/LendingSimulation/CalculateBorrowingDataResponse", name = "calculateBorrowingDataResponse")
public JAXBElement<CalculateBorrowingDataResponseType> createCalculateBorrowingDataResponse(CalculateBorrowingDataResponseType value) {
return new JAXBElement<CalculateBorrowingDataResponseType>(_CalculateBorrowingDataResponse_QNAME, CalculateBorrowingDataResponseType.class, null, value);
}
为了让JAXBContext处理ObjectFactory
类,您需要包含在传递的类数组中以创建JAXBContext:
JAXBContext jc = JAXBContext.newInstance(CalculateBorrowingDataResponseType.class, ObjectFactory.class);
或者在生成的类的包名上创建JAXBContext:
String contextPath = CalculateBorrowingDataResponseType.class.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance(contextPath);
完整示例
package forum8809406;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("forum8809406");
jc.generateSchema(new SOR());
}
private static class SOR extends SchemaOutputResolver {
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(suggestedFileName);
return result;
}
}
}
我得到以下生成的XML架构,其中包含calculateBorrowingDataResponse
元素:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema
version="1.0"
elementFormDefault="qualified"
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:element name="calculateBorrowingDataResponse" type="tns:CalculateBorrowingDataResponseType"/>
</xs:schema>
答案 2 :(得分:0)
为什么你认为在roundrip'schema1-&gt; XJC-&gt; schema2'schema1和schema2应该是相同的?你有XJC为'calculateBorrowingDataResponse'元素生成的Java类吗?我不这么认为 - 那么你期望它如何在生成的模式中呢?