我知道这个问题之前已被问过,但他们都没有问我到底要找什么。
我想知道如何通过groovy脚本对RESTful
模式文件验证XSD
服务响应(application / xml)。是的,我的本地磁盘上有xsd文件,但我似乎无法理解如何提供响应作为验证的输入?
我创建了一个样本但缺少一个关键部分。任何人都可以帮忙吗?
import com.eviware.soapui.support.XmlHolder
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.*
// setup validator
Validator validator;
def url = 'C:\\Documents and Settings\\schema\\sclBase.xsd'
log.info url
URI uri = new URI(url);
InputStream inp = uri.toURL().openStream();
try
{
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource entitySs = new StreamSource(inp);
Schema schema = factory.newSchema(entitySs);
assert(schema != null);
validator = schema.newValidator();
def response = new XmlHolder( messageExchange.responseContentAsXml )
log.info response
validator.validate(new StreamSource(new StringReader(response)))
}
finally
{
inp.close();
inp = null;
}
我在这里遇到的错误是“ - >索引2处不透明部分的非法字符:C:\ Documents and Settings \ schema \ sclBase.xsd”
答案 0 :(得分:2)
假设您在名为response
import javax.xml.XMLConstants
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
String response = messageExchange.responseContent
new File( 'C:\\Documents and Settings\\schema\\sclBase.xsd' ).withReader { xsd ->
SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI )
.newSchema( new StreamSource( xsd ) )
.newValidator()
.validate( new StreamSource( new StringReader( response ) ) )
}
应该这样做。您正在关闭输入流,然后从中读取输入流(但您没有说明您得到了什么错误)