-
Jan 22nd, 2011, 05:16 PM
#1
looking for a simple jaxb2 example that validates and pretty prints
Hi
I am new to spring. for the life of me I can not figure out how to configure
org.springframework.oxm.jaxb.Jaxb2Marshaller so that it pretty prints and
validates. Any idea what I need to add to my configuration?
<bean id="xmlHelper" class="com.bdc..XMLHelper">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
</bean>
<oxm:jaxb2-marshaller id="jaxbMarshaller" >
<oxm:class-to-be-bound
name="com.bcd.xsd.FindAccountRequest" />
<oxm:class-to-be-bound
name="com.bdc.xsd.FindAccountResponse" />
<oxm:class-to-be-bound
name="com.bdc.xsd.SearchRequest" />
<oxm:class-to-be-bound
name="com.bdc.xsd.SearchResponse" />
</oxm:jaxb2-marshaller>
I figure out how to get pretty printing, how ever I had to this in Java. This seems like something I should be able to configure ?
// have marshaller pretty print XML
Jaxb2Marshaller jaxb2Marshaller = (Jaxb2Marshaller)marshaller;
HashMap<String,Object> properties = new HashMap<String,Object>();
properties.put(javax.xml.bind.Marshaller.JAXB_FORM ATTED_OUTPUT,
Boolean.TRUE);
jaxb2Marshaller.setMarshallerProperties(properties );
Here is my hack to try and get validation to work. The code compiles and runs, how ever it does not validate
private void setUpValidation() {
Resource schemaResources[];
schemaResources = new Resource[5];
FileSystemResource accountLookUpSchema =
new FileSystemResource("../XSD/AccountLookUp.xsd");
FileSystemResource searchSchema =
new FileSystemResource("../XSD/Search.xsd");
schemaResources[0] = accountLookUpSchema;
schemaResources[1] = searchSchema;
//
// this is a horrible hack
// turns out there is only 1 obj implementation. It implements both the marshall and
// unmarshall interfaces. setSchemas() is defined on the marshall interface :-(
//
Jaxb2Marshaller jaxb2Marshaller = (Jaxb2Marshaller)unmarshaller;
jaxb2Marshaller.setSchemas(schemaResources);
}
I was hoping I could not find an escape hatch. I.E. some way way to cast to the standard javax.xml.bind.Unmarshaller and javax.xml.bind.Marshaller. At least I could work around these problems using the JDK 1.6 impl of jaxb2
as follows
static {
try {
String generatedPackageName = "com.bdc.lava";
JAXBContext jc;
jc = JAXBContext.newInstance(generatedPackageName);
unmarshaller = jc.createUnmarshaller();
} catch (JAXBException e) {
err.println(" [ERROR] " + e);
e.printStackTrace();
}
// set up validation
SchemaFactory factory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = null;
String xsdFile = "xsds/TotalsRequest.xsd";
try {
schema = factory.newSchema(new File(xsdFile));
} catch (SAXParseException spe) {
err.println(
"[ERROR] Parse error file:" + xsdFile
+ " line:" + spe.getLineNumber()
+ " col:" + spe.getColumnNumber()
+ "\n\t" + spe.getMessage()
);
System.exit(1);
} catch (SAXException e) {
err.println(" [ERROR] " + e);
e.printStackTrace();
System.exit(1);
}
unmarshaller.setSchema(schema);
}
thanks
Andy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules