jaxb - 无法从包含其他xsd的XSD解组

时间:2014-09-26 21:48:01

标签: jaxb xsd

我有一个XSD(copy-metainfo.xsd),其中包含其他XSD(test-component-types.xsd)。我已经从copy-metainfo生成了工件,但我试图通过使用jaxb api从copy-metainfo.xsd解组。

这是我的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中。

  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:无法解析名称&#39; test:testComponentType&#39;到(n)&#39;类型定义&#39;成分

第22行是&#39;&#39;

这是一个我试图从jar中解组的XML:

    <?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中并且&#39; COPY_METAINFO_SCHEMA&#39;变量指向正确的位置。如果我直接在copy-metainfo.xsd中指定testComponentType而不是包含test-component-types.xsd,那么它就可以工作。

XSD或XML或java代码有什么问题吗?

1 个答案:

答案 0 :(得分:3)

XML是正确的(语义和语法),我通过XMLSpear验证了它

您必须将所有XSD添加到架构验证器..test-component-types.xsd和copy-metainfo.xsd

InputStream xsdStream = CopyMetaInfos.class.getClassLoader().getResourceAsStream(COPY_METAINFO_SCHEMA);

通过这种方式,您只添加了一个XSD并且缺少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);