I am trying to work out how to unmarshall and XML document to a Java document.
The top of the xml document looks like this
<xs:myData xmlns:xs="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com example.xsd ">
There is a schema file whose top section looks like this:
<schema targetNamespace="http://www.example.com"
elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.example.com">
I would like to unmarshall the xml document using Spring/JaxB and eventually convert it to a JPA object. I am not sure how to go about so i looked for examples on google and came up with this http://thoughtforge.net/610/marshalling-xml-with-spring-ws-and-jaxb/
I understand most of it except how or where the schema is used.
I have seen other examples where the schema is explicitly specified, i.e.
SchemaFactory schemaFac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema sysConfigSchema = schemaFac.newSchema(
new File("example.xsd"));
unmarshaller.setSchema(sysConfigSchema);
RootElement root = (RootElement)unmarshaller.unmarshal(
new File("example1.xml"));
- How is the schema shown in the first link used to validate the xml document?
- Are there any disadvantages to using Spring’s jaxb2Marshaller as opposed to direct use of JAXB?
- What is the effect of having the namespace next to the XmlElement annotation? (See the Person class)
I would appreciate any more examples showing Spring/REST with unmarshalling with schema validation.
Thanks
xsiattribute to dereference XSD, load it and use for validation. Perhaps that was done to disable automatic validation, otherwise it would be problematic to switch it off 🙂Jaxb2Marshallerwas obviously added to implement the same interfaceorg.springframework.oxm.Marshaller(which is implemented also byCastorMarshaller,JibxMarshaller, …). It is very powerful and allows you to tuneJAXBContextin very flexible way (I can’t imagine the scenario when provided API is not enough). From pattern point of newJaxb2Marshalleris a builder, so it does not add anything to core JAXB functionality. But there are some evident advantages. For example, schema loading is very simple. In the article the Spring context refers theperson.xsd(<property name="schema" value="classpath:schema/person.xsd"/>) which one need to put into resources explicitly. Then JAXB marshaller/unmarshaller will use this schema to validate XML when XML is generated/loaded.@XmlElement(..., namepsace="xxx")will automatically generate this XML element with a specified namespace. It’s rare case if somebody does not use namespaces. I would say writing XSD without namespaces is not normal, as you want to avoid the element name collision.RestTemplateis very simple. You need to be sure that JAXB runtime is in your classpath (JDK 6 already has it) and your bean is annotated with@XmlRootElement. Then just usePerson person = restTemplate.getForObject(restServiceUrl, Person.class),