我花了很长时间试图弄清楚如何验证XML文件,并且我已经使用预先生成的XSD架构。但是,我希望在没有创建文件的情况下动态生成模式,基于我指定的注释类,我试图不向SchemaFactory指定任何参数,但它似乎只是创建一个空模式(请参阅注释)下文)。
以下是我用于JAXB读取和写入XML文件的两个类。
这是XMLTranslationWrapper类的代码:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(namespace = "my.package.namespace")
public class XMLTranslationWrapper {
@XmlElementWrapper(name = "TRANSLATIONS")
@XmlElement(name = "TRANSL")
public ArrayList<XMLTranslationNode> translations;
public XMLTranslationWrapper(){
translations = new ArrayList<XMLTranslationNode>();
}
public void setTranslations(ArrayList<XMLTranslationNode> translations){
this.translations = translations;
}
public XMLTranslationNode getTranslation(String code){
for(XMLTranslationNode transl : translations){
if(transl.getCode().equals(code))
return transl;
}
return null;
}
public void addTranslation(XMLTranslationNode translation){
this.translations.add(translation);
}
}
这是XMLTranslationNode类的代码:
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "TRANSL")
@XmlType(propOrder = { "severity", "translation" })
public class XMLTranslationNode {
private String severity;
private String code;
private String translation;
@XmlElement(name="SEVERITY")
public String getSeverity(){
return this.severity;
}
public void setSeverity(String severity){
this.severity = severity;
}
@XmlAttribute(name="CODE")
public String getCode(){
return this.code;
}
public void setCode(String code){
this.code = code;
}
@XmlElement(name="TRANSLATION")
public String getTranslation(){
return this.translation;
}
public void setTranslation(String translation){
this.translation = translation;
}
}
这是我用来生成预生成的XSD架构的代码:
public class generateSchema {
public static void main(String[] args) {
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(XMLTranslationWrapper.class);
SchemaOutputResolver sor = new MySchemaOutputResolver();
jaxbContext.generateSchema(sor);
} catch (JAXBException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static class MySchemaOutputResolver extends SchemaOutputResolver {
public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
File file = new File(suggestedFileName);
StreamResult result = new StreamResult(file);
System.out.println(file.toURI().toURL().toString());
result.setSystemId(file.toURI().toURL().toString());
return result;
}
}
}
最后,这是一个示例XML,可用于测试代码:
<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<ns2:xmlTranslationWrapper xmlns:ns2="my.package.namespace">
<TRANSLATIONS>
<TRANSL CODE="123">
<SEVERITY>Information</SEVERITY>
<TRANSLATION>ABC</TRANSLATION>
</TRANSL>
</TRANSLATIONS>
</ns2:xmlTranslationWrapper>
如何在不创建与使用预生成的XSD架构等效的文件的情况下动态生成XSD架构?
答案 0 :(得分:5)
如同承诺的那样。这个想法很简单:
我认为你不能做得更好。 JAXB的schemagen
内部结构似乎与javax.xml.validation.Schema
不兼容。因此,创建DOM然后解析它是最简单的方法。
public class DynamicSchemaTest {
@XmlRootElement
public static class A {
@XmlAttribute(required = true)
public String name;
public A() {
}
public A(String name) {
this.name = name;
}
}
@Test(expected = MarshalException.class)
public void generatesAndUsesSchema() throws JAXBException, IOException,
SAXException {
final JAXBContext context = JAXBContext.newInstance(A.class);
final DOMResult result = new DOMResult();
result.setSystemId("schema.xsd");
context.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(String namespaceUri,
String suggestedFileName) {
return result;
}
});
@SuppressWarnings("deprecation")
final SchemaFactory schemaFactory = SchemaFactory
.newInstance(WellKnownNamespace.XML_SCHEMA);
final Schema schema = schemaFactory.newSchema(new DOMSource(result
.getNode()));
final Marshaller marshaller = context.createMarshaller();
marshaller.setSchema(schema);
// Works
marshaller.marshal(new A("works"), System.out);
// Fails
marshaller.marshal(new A(null), System.out);
}
}