2011-07-18 36 views
1

我想使用JAXB解組一些XML,但我似乎得到錯誤「無法解析名稱xxx到(n)'類型定義'組件。」當我打包含sf.newSchema(...)行:JAXB:發生錯誤「無法將名稱xxx解析爲(n)'類型定義'組件。」

JAXBContext jc = JAXBContext.newInstance("some.package.name"); 

Unmarshaller unmarshaller = jc.createUnmarshaller(); 

SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); 

Collection<Source> sources = new ArrayList<Source>(); 

sources.add(new StreamSource(new File("dog.xsd"))); 
sources.add(new StreamSource(new File("cat.xsd"))); 
sources.add(new StreamSource(new File("cow.xsd"))); 
sources.add(new StreamSource(new File("horse.xsd"))); 
sources.add(new StreamSource(new File("mouse.xsd"))); 

Schema schema = sf.newSchema(sources.toArray(new Source[0])); // SAXParseException thrown here 
unmarshaller.setSchema(schema); 

unmarshaller.unmarshal(socket.getInputStream()); 

的異常跟蹤看起來是這樣的:

org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'xxx:xxx' to a(n) 'type definition' component. 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195) 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:2537) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:2528) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:1396) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseSimpleContent(XSDComplexTypeTraverser.java:373) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(XSDComplexTypeTraverser.java:249) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseGlobal(XSDComplexTypeTraverser.java:160) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(XSDHandler.java:1255) 
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:579) 
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552) 
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:519) 
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:485) 
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:211) 
    ... 

我有同樣的問題,當我使用的XJC編譯器從每個上述XSD生成源代碼,但設法通過使用目錄(xjc -catalog ...)來解決該問題。

有沒有人知道目錄的編程等價物是什麼?

+0

什麼是來自'SAXParseException'的詳細消息? XSD文件的外觀如何? –

+0

「xxx」是否出現在任何模式中? –

+0

「目錄的編程等效」可能是一個EntityResolver。 –

回答

1

我得到了類似的問題,因爲我的XSD正在導入另一個XSD。解決它的方法是獲取所有導入的XSD,並將其包含在構建模式中。

例如

InputStream imported2Schema = ...getResourceAsStream("/com/path/to/Imported2.xsd"); 
Source imported2Source = new StreamSource(imported2Schema); 
InputStream imported1Schema = ...getResourceAsStream("/com/path/to/Imported1.xsd"); 
Source imported1Source = new StreamSource(imported1Schema); 
InputStream metadataSchema = ...getResourceAsStream("/com/path/to/metadata.xsd"); 
Source metadataSource = new StreamSource(metadataSchema); 
Source[] schemaSources = new Source[] {imported2Source, imported1Source, metadataSource}; 
Schema schema = sf.newSchema(schemaSources); 
相關問題