I'm trying to learn how to marshal & unmarshal XML to/from Java code, using Spring. My sample code works for unmarshalling a XML file into my Java classes, based on a XSD file (for validation).
What I'm stuck on and not finding much info on is how to marshal based on the XSD schema file?
Code extracts are:
In the applicationContext.xml file I have:
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan" value="com.company" />
<property name="schema" value="file:test1.xsd" />
</bean>
My main class refers to the jaxb2Marshaller bean.
In my main class I have this to write the XML:
private void writeXML() {
crashScene.getWorkZoneInfo().getWorkZoneDetails().setWorkZoneSpeedLimit(45); // updates the data previously read in by unmarshalling.
FileOutputStream fos;
try {
fos = new FileOutputStream("test_output.xml");
marshaller.marshal(crashScene, new StreamResult(fos));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Since I have a schema set in the applicationContext, when trying to write the XML I get an Exception:
org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException- with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 0; columnNumber: 0; cvc-complex-type.2.4.a: Invalid content was found starting with element 'FormNum'. One of '{XmlSchemaVersion}' is expected.]
If I try writing the XML without using a schema defined, then marshalling works.
How can I write out the XML based on the schema definition??
Many Thanks!
Chris
I hope this page could help you.
http://docs.spring.io/spring-ws/sites/1.5/reference/html/oxm.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.