jaxb - 无法从包含其他xsd的XSD解组 [英] jaxb - Unable to unmarshall from XSD which included other xsd
问题描述
我有一个XSD(copy-metainfo.xsd),其中包含其他XSD(test-component-types.xsd)。我已经从copy-metainfo生成了工件,但是我试图通过使用jaxb api从copy-metainfo.xsd解组。
I have an XSD(copy-metainfo.xsd) that includes other XSD (test-component-types.xsd). I already generated artifacts from copy-metainfo, but I am trying to unmarshall from copy-metainfo.xsd by using jaxb api.
这里是我的xsd。
test-component-types.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:complexType name="testComponent">
<xs:attribute name="type" type="testComponentType"/>
</xs:complexType>
<xs:simpleType name="testComponentType">
<xs:restriction base="xs:string">
<xs:enumeration value="TYPE1"/>
<xs:enumeration value="TYPE2"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
copy-metainfo.xsd
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:test="http://xmlns.test.com/cie/test/copy-metainfo"
targetNamespace="http://xmlns.test.com/cie/test/copy-metainfo">
<xs:include schemaLocation="test-component-types.xsd"/>
<xs:element name="copy-metainfos" type="test:copy-metainfos-type" />
<xs:complexType name="copy-metainfos-type">
<xs:sequence>
<xs:element name="copy-metainfo" type="test:copy-metainfo" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="copy-metainfo">
<xs:sequence>
<xs:element name="file-paths" type="test:FilePaths" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="test-type" type="test:testComponentType"/>
</xs:complexType>
<xs:complexType name="FilePaths">
<xs:sequence>
<xs:element name="location" type="test:Location" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Location">
<xs:attribute name="src" type="xs:string"/>
</xs:complexType>
</xs:schema>
当我尝试使用以下代码从copy-metainfo.xsd解组时,出现错误。这些XSD包含在同一个jar中。
When I try to unmarshall from copy-metainfo.xsd with the following code, I get an error. These XSDs are contained in a same jar.
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{CopyMetaInfos.class});
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
StreamSource xsdSource = new StreamSource(xsdStream);
Schema schema = schemaFactory.newSchema(xsdSource);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return (CopyMetaInfos) unmarshaller.unmarshal(is);
错误:
org.xml.sax.SAXParseException; lineNumber:22; columnNumber:64; src-resolve:无法将名称'test:testComponentType'解析为(n)'类型定义'组件。
org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 64; src-resolve: Cannot resolve the name 'test:testComponentType' to a(n) 'type definition' component.
第22行是''。
这是一个我试图从一个罐子里解组的XML:
This is a XML that I an trying to unmarshall from a jar:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<copy-metainfos xmlns="http://xmlns.test.com/cie/test/copy-metainfo">
<copy-metainfo test-type="TYPE1">
<file-paths>
<location src="common"/>
</file-paths>
</copy-metainfo>
</copy-metainfos>
我确认XSD在jar中,'COPY_METAINFO_SCHEMA'变量指向正确的位置。如果我直接在copy-metainfo.xsd中指定testComponentType而不是包含test-component-types.xsd,那么它可以工作。
I verified that the XSDs are in the jar and 'COPY_METAINFO_SCHEMA' variable points to a correct location. If I specify testComponentType directly in the copy-metainfo.xsd instead of including test-component-types.xsd, then it works.
XSD是否有任何问题或XML或Java代码?
Is there anything wrong with the XSD or XML or java code?
推荐答案
XML是正确的(语义和语法),我通过 XMLSpear
XML is correct (semantically and syntactically), i validated it by XMLSpear
您必须将所有XSD添加到架构验证器.test-component-types .xsd和copy-metainfo.xsd
You must add all XSD to schema validator ..test-component-types.xsd and copy-metainfo.xsd
InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
这样你只添加了一个XSD并且缺少 test的定义: testComponentType
in this way you added only one XSD and missing the definition of test:testComponentType
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{CopyMetaInfos.class});
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//Source copy-metainfo.xsd
InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);
StreamSource xsdSource = new StreamSource(xsdStream);
//Source test-component-types.xsd
InputStream xsdStreamTest = CopyMetaInfos.class.getClassLoader().getResourceAsStream(TEST_COMPONENT_TYPES);
StreamSource xsdSourceTest = new StreamSource(xsdStreamTest);
Schema schema = schemaFactory.newSchema(new StreamSource[]{xsdSource,xsdSourceTest});
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema(schema);
return (CopyMetaInfos) unmarshaller.unmarshal(is);
这篇关于jaxb - 无法从包含其他xsd的XSD解组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!