开发者

JAXB: Getting the error "Cannot resolve the name xxx to a(n) 'type definition' component."

I'm trying to use JAXB to unmarshal some XML but I seem to get the error "Cannot resolve the name xxx to a(n) 'type definition' component." when I hit the line containing sf.newSchema(...):

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

Unmarshaller unmarshaller = jc.cr开发者_StackOverflow中文版eateUnmarshaller();

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());

The exception trace looks like this:

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)
    ...

I had the same issue when I used the XJC compiler to generate sources from each of the above XSDs, but managed to resolve the issue by using a catalog (xjc -catalog ...).

Does anyone know what the programmatic equivalent of a catalog is?


I got a similar issue because my XSD was importing another XSD. The way to solve it is by getting all the imported XSDs, and including it in building the schema.

e.g.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜