java - Validating an xml against xsd that was used to generate Beans -
i generate beans couple of xsd via ant-build.
when unmarshalling xml, validate one. far know, there way beans themself, 1 has this:
jaxbcontext context = jaxbcontext.newinstance(bean.class); schemafactory sf = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri); schema schema = sf.newschema(new file("whatever.xsd")); unmarshaller unmarshaller = context.createunmarshaller(); unmarshaller.setschema(schema); unmarshaller.seteventhandler(validationhandler); return (bean) unmarshaller.unmarshal(givenxmlstring);
my problem new file("whatever.xsd")
. don't want hardcode url xsd, might change later (i.e. refactoring project) , break @ runtime, because 1 forgot (or didn't know) change url.
idea:
idea have copy xsd same folder generated beans , use packagename of 1 bean generate url @ runtime.
any better ideas?
instead of sf.newschema(file)
can use sf.newschema(source[])
1 of javax.xml.transform.source
implementations, e.g. javax.xml.transform.stream.streamsource.
Comments
Post a Comment