我知道在编译时创建模式(JAXB)但是如何在运行时获取此信息。
模特课:
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;
}
如何基于JAXB元数据Model.java?
在运行时创建Schema实例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())));
}
}
答案 0 :(得分:0)
解决方案(nazgul-core-xmlbinding-spi-jaxb)在这里找到: JAXB Validation Without Schema
取代:
toReturn.setSystemId("");
使用:
toReturn.setSystemId(suggestedFileName);