【问题标题】:Validating XML document against XSD in Groovy when they are available at certain URLs当 XML 文档在某些 URL 上可用时,在 Groovy 中针对 XSD 验证 XML 文档
【发布时间】:2019-04-10 16:31:50
【问题描述】:
任何人都可以帮助我使用 sn-p 来验证带有 XSD 的 XML 模式,当它们在某些 URL 上可用时......
【问题讨论】:
标签:
validation
url
groovy
xsd
schema
【解决方案1】:
您可以这样做(改编自 the documentation,使用 google 找到 xml 和 xsd url)
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
xsdUrl = 'http://abbot.sourceforge.net/doc/abbot.xsd'
xmlUrl = 'http://abbot.sourceforge.net/src/example/SimpleApplet.xml'
new URL( xsdUrl ).withInputStream { xsd ->
new URL( xmlUrl ).withInputStream { xml ->
SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI )
.newSchema( new StreamSource( xsd ) )
.newValidator()
.validate( new StreamSource( xml ) )
}
}