how can i unmarshall in jaxb and enjoy the schema validation without using an explicit schema file

Posted by ekeren on Stack Overflow See other posts from Stack Overflow or by ekeren
Published on 2010-04-08T22:06:30Z Indexed on 2010/04/09 0:03 UTC
Read the original article Hit count: 376

Filed under:
|
|

I am using jaxb for my application configurations

I feel like I am doing something really crooked and I am looking for a way to not need an actual file or this transaction.

As you can see in code I:

1.create a schema into a file from my JaxbContext (from my class annotation actually) 2.set this schema file in order to allow true validation when I unmarshal

JAXBContext context = JAXBContext.newInstance(clazz);
Schema mySchema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);
jaxbContext.generateSchema(new MySchemaOutputResolver()); // ultimately creates schemaFile   
Unmarshaller u = m_context.createUnmarshaller();
u.setSchema(mySchema);
u.unmarshal(...);

do any of you know how I can validate jaxb without needing to create a schema file that sits in my computer?

Do I need to create a schema for validation, it looks redundant when I get it by JaxbContect.generateSchema ?

How do you do this?

© Stack Overflow or respective owner

Related posts about jaxb

Related posts about java