当某些URL可用时,任何人都可以帮我使用XSD验证带有XSD的XML模式吗?
你可以这样做(改编自the documentation,使用google找到xml和xsd网址)
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 ) )
}
}