有没有最好的方法来验证生成的 XML 与 Java 中的 XSD
Is there any best way to validate generated XML against XSD in Java
我正在使用 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
有没有更好的方法来做到这一点。我正在经历这个 link
What's the best way to validate an XML file against an XSD file?。大多数答案来自 2008 年,我只是好奇现在是否有更好的方法可以做到这一点。
您正在使用 JAXB,这是在 Java 中使用 XML 的 "standard" 方式。此示例代码显示了多个生成的包(由 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 是线程安全的,可以创建一次并由多个线程永久重用,通常在静态初始化程序中。通常应该为每个文档创建一个新的编组器。
我正在使用 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
有没有更好的方法来做到这一点。我正在经历这个 link What's the best way to validate an XML file against an XSD file?。大多数答案来自 2008 年,我只是好奇现在是否有更好的方法可以做到这一点。
您正在使用 JAXB,这是在 Java 中使用 XML 的 "standard" 方式。此示例代码显示了多个生成的包(由 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 是线程安全的,可以创建一次并由多个线程永久重用,通常在静态初始化程序中。通常应该为每个文档创建一个新的编组器。