我正在使用Javax Marshaller生成xml。我正在寻找验证生成的XML反对XSD的方法。我发现gradle插件可以做到这一点。但是我不能使用这个插件,因为它给了一些错误
Could not resolve gradle.plugin.com.github.ramonwirsch:fopRenderer:0.1.7
我的build.gradle文件:
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.ramonwirsch:fopRenderer:0.1.7"
}
}
apply plugin: "com.github.ramonwirsch.FopRenderer"
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility=1.8
targetCompatibility=1.8
project.ext {
app_name='XSD-XML'
build_date=new Date().format("yyyyMMddHHmm")
release_number='R1.0'
rc_build="${System.env.BUILD_NUMBER}"//look for BUILD_NUMBER in env (set by Jenkins)
generatedSrcDir = file("$buildDir/generated-src")
jaxb_schema='src/main/resources/xsd'
jaxbTargetDir = generatedSrcDir
}
repositories {
// removed corporate repository details for security
jcenter()
}
configurations {
jaxb
}
dependencies {
// Needed to generate JAX binding code
jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.4-1'
}
def generatedResources = 'src/main/generated'
task jaxb(){
description 'Converts xsds to classes'
def jaxbTargetDir = file( generatedResources )
jaxbTargetDir.deleteDir()
doLast {
jaxbTargetDir.mkdirs()
ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath)
ant.jaxbTargetDir = jaxbTargetDir
ant.xjc(destdir: '${jaxbTargetDir}', package: 'com.pavan.xml.java.binding') {
schema(dir:'src/main/resources/xsd', includes: '*.xsd')
}
}
}
task generateSources(dependsOn: jaxb) {
description 'generates the resources defined by the external xsd\'s'
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/generated']
}
}
}
compileJava.dependsOn jaxb
有没有更好的方法可以做到这一点。我正在通过这个链接
What's the best way to validate an XML file against an XSD file?。大多数答案来自2008年。我很好奇,现在是否有更好的方法可以做到这一点。
最佳答案
您正在使用JAXB,这是在Java中使用XML的“标准”方式。此示例代码显示了一个以上的软件包(由xjc生成)Java类和一个以上的针对您进行验证的XSD,以及在Marshaller上设置了属性。
// This part can be in a static initializer, with `jaxbContext` and `schema` as static variables.
JAXBContext jaxbContext = JAXBContext.newInstance("package1:package2");
try (InputStream xsdStream1 = EraMarshallingService.class.getResourceAsStream("/a.xsd");
InputStream xsdStream2 = EraMarshallingService.class.getResourceAsStream("/b.xsd");
InputStream xsdStream3 = EraMarshallingService.class.getResourceAsStream("/c.xsd");) {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new Source[] { new StreamSource(xsdStream1), new StreamSource(xsdStream2),
new StreamSource(xsdStream3));
}
// This part should be done for each document.
Marshaller marshaller = getJaxbContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setSchema(schema);
marshaller.marshal(jaxbRootElement, whereTheOutputIsGoing);
请注意,JAXBContext和Schema是线程安全的,通常可以在静态初始化程序中创建一次并由多个线程永久重用。通常应该为每个文档创建一个新的编码器。
关于java - 是否有最佳方法针对Java中的XSD验证生成的XML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40094013/