I know about creating schemas at compile time (JAXB ) but how can i get this information at runtime .
Model class:
package pl.kkrzeminski;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Model {
@XmlElement(required = true)
private String text;
}
How can i create at runtime instance of Schema based on JAXB metadata Model.java?
package pl.kkrzeminski;
import org.xml.sax.SAXException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws SAXException, IOException {
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
// TODO how can i create at runtime instance of Schema based on Model.java?
javax.xml.validation.Schema schema = sf.newSchema(new Source[]{});
schema.newValidator().validate(new StreamSource(new ByteArrayInputStream("<model><text>test</text></model>".getBytes())));
}
}
Solution (nazgul-core-xmlbinding-spi-jaxb) found here: JAXB Validation Without Schema
replace:
toReturn.setSystemId("");
with:
toReturn.setSystemId(suggestedFileName);