【问题标题】:Validating XML against XSD in soapui在 soapui 中针对 XSD 验证 XML
【发布时间】:2014-02-18 09:39:27
【问题描述】:

我知道这个问题以前在这里被问过,但他们都没有确切地问我在找什么。

我想知道我们如何通过 groovy 脚本针对 XSD 架构文件验证 RESTful 服务响应(应用程序/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;
}

我在这里遇到的错误是 "->Illegal character at opaque part at index 2: C:\Documents and Settings\schema\sclBase.xsd"

【问题讨论】:

    标签: xml groovy xsd soapui xml-validation


    【解决方案1】:

    假设您在名为 response 的字符串中有 REST 响应

    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 ) ) )
    
    }
    

    应该这样做。你在读取之前关闭了输入流(但你没有说你得到了什么错误)

    【讨论】:

    • 但是我们在哪里使用response 变量?我使用了这个脚本,它给了我错误"->src-resolve: Cannot resolve the name tns:id to a(n) 'element declaration' component"
    • 抱歉,您的问题真的是“如何将响应转换为字符串”
    • 你现在说的那个错误似乎表明你的xsd有错误
    • 试试String response = messageExchange.requestContent
    • 你试过我 19 小时前所说的吗?
    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 2012-07-26
    • 2012-09-20
    • 2011-10-12
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    相关资源
    最近更新 更多