gpt4 book ai didi

xml - 使用多个 XSD 针对 WSDL 验证 SOAP 消息

转载 作者:数据小太阳 更新时间:2023-10-29 01:57:35 25 4
gpt4 key购买 nike

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

有谁知道在给定 WSDL 和多个 XSD 的情况下验证 XML 文档的相对简单的方法?

最佳答案

在之前的项目中,我通过解析 WSDL 文件并从中提取模式解决了这个问题。代码类似于以下内容,它假定 WSDL 已以某种方式读入源变量“wsdlSource”,并且导入的 namespace 已在“schema”元素中声明。在启动时执行一次然后在 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();

关于xml - 使用多个 XSD 针对 WSDL 验证 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8979044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com