使用多个 XSD 针对 WSDL 验证 SOAP 消息 [英] Validating SOAP message against WSDL with multiple XSD's

查看:23
本文介绍了使用多个 XSD 针对 WSDL 验证 SOAP 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网上浏览了好几个小时,试图找到一种简单的方法来根据 WSDL 验证完整的 SOAP 消息.我知道有多种方法可以使用各种 Web 服务框架来做到这一点,但我不想这样做,因为要求只是验证一段 XML.我可以针对模式进行验证,尽管我遇到的问题是有许多模式导入到 WSDL 中,但我不知道我应该针对哪个模式进行验证.我可以编写一些实用程序来首先处理 WSDL 和响应以确定要验证哪个 XSD,但我认为这可以使用已建立的库作为单行程序完成!

I've been looking around the net for a good few hours now, trying to find a simple way to validate a full SOAP message against a WSDL. I am aware that there are ways to do this with the various Web Service frameworks out there, but I don't want to do this as the requirement is simply to validate a piece of XML. I could validate against the schema, although the problem I have is that there are a number of schemas imported into the WSDL and I don't know which one I should be validating against. I could write some utility to first process the WSDL and the response to determine which XSD to validate against, but I presumed this could be done as a one-liner using an established library!

有人知道一种相对简单的方法来验证给定 WSDL 和多个 XSD 的 XML 文档吗?

Does anyone know of a relatively straightforward way to validate an XML document given a WSDL and multiple XSD's?

推荐答案

在之前的一个项目中,我通过解析 WSDL 文件并从中提取模式解决了这个问题.代码类似于以下内容,它假定 WSDL 已以某种方式读入源变量wsdlSource",并且导入的命名空间在schema"元素中声明.最好在启动时执行一次,然后在 SOAPHandler 中进行验证.

In a previous project I solved this problem by parsing the WSDL-file and extracting the schemas from it. The code was something like the following, it assumes that the WSDL has been read into the Source variable "wsdlSource" in some way and that the imported namespaces are declared in the "schema"-element. It would probably be a good idea to have this performed once on startup and then do the validation in a SOAPHandler.

    //First create a document from the WSDL-source
    DocumentBuilder db = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
    Document wsdlDoc = db.newDocument();

    TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(wsdlSource, new DOMResult(wsdlDoc));

    //Now get the schemas from the WSDL 
    NodeList schemaNodes = wsdlDoc.getElementsByTagNameNS(
            XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");

    int nrSchemas = schemaNodes.getLength();

    Source[] schemas = new Source[nrSchemas];

    for (int i = 0; i < nrSchemas; i++) {
        schemas[i] = new DOMSource(schemaNodes.item(i));
    }

    SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    //Now we have a schema that can validate the payload
    Schema schema = schemaFactory.newSchema(schemas);
    Validator validator = schema.newValidator();

这篇关于使用多个 XSD 针对 WSDL 验证 SOAP 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆