Loading Schema in Spring XML
I'm trying to load an XSD file as a Schema instance in my application context XML file. We're building it in java code, but I'd like to be injecting it.
Here's what the user class looks like if that'll clarify things.
class XmlBuilder {
...
Schema schema; // set by spring
public String createXml(Object param1) {
// create xml
Validator validator = schema.newValidator();
try {
validator.validate(documentSS);
} catch (SAXException e) {
// log and convert to a proper exception
}
return xml;
}
}
The schema is in the app's classpath, I'm just having trouble loading it and creating the schema object. I can get this far
<bean id="schemaFactory" class="javax.xml.validatio开发者_开发技巧n.SchemaFactory" factory-method="newInstance">
<constructor-arg value="http://www.w3.org/2001/XMLSchema"/>
</bean>
<bean id="xmlBuilder" class="XmlBuilder">
<property name="schema">
<bean factory-bean="schemaFactory" factory-method="newSchema">
<!-- missing bit -->
</bean>
</property>
</bean>
But I'm stuck on loading the file and passing it into the factory method.
I ended up using the ResourceSource
class in 'org.springframework.ws:spring-xml'
<property name="schema">
<bean class="org.springframework.xml.transform.ResourceSource">
<constructor-arg value="classpath:/schema.xsd"/>
</bean>
</property>
精彩评论