是否可以使用groovy xmlslurper或xmlparser根据xsd验证xml?
我拥有的XML是一个简单的字符串。
最佳答案
不是用XmlSlurper
或XmlParser
的,但你可以still call the standard Java way from Groovy:
从该链接改编并粘贴到此处的代码:
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
String xmlString = ''' ... your xml ... '''
String xsdString = ''' ... your xsd ... '''
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
def schema = factory.newSchema(new StreamSource(new StringReader(xsdString)))
def validator = schema.newValidator()
validator.validate(new StreamSource(new StringReader(xmlString)))