gpt4 book ai didi

java - 使用 validator 针对 xsd 验证soap xml

转载 作者:行者123 更新时间:2023-12-01 10:27:23 27 4
gpt4 key购买 nike

我想根据给定的xsd验证soap请求xml,请求可能很大,所以我不能只提取soap主体的内容并进行验证,而是进行基于流的验证。我已经为soap主体(请求部分)制作了xsd,并且它不包含有关soap header 的任何信息,因此验证失败,我所做的是将soap模式导入到我的xsd中,以便验证程序可以识别soap header 。它有效,但即使肥皂体不包含任何东西,验证也会成功。我们如何在 xsd 中指定肥皂体应至少包含特定元素?我修改后的xsd粘贴在下面。谢谢。

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="qualified" targetNamespace="urn:sample"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="sample">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/envelope/"
schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
<xsd:complexType name="type1">
<xsd:sequence>
<xsd:element name="item" type="tns:type2" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="type3">
<xsd:sequence>
<xsd:element name="item" type="tns:type4" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="element1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RETURN" type="tns:type3" minOccurs="0"/>
<xsd:element name="HEADER" type="tns:type1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="element2">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="RETURN" type="tns:type3" minOccurs="0"/>
<xsd:element name="ERRORS" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

最佳答案

尝试类似的操作:

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
FileInputStream fileInputStream = new FileInputStream(new File("Simple.xsd"));
Schema schema = factory.newSchema(new StreamSource(fileInputStream));
Validator val = schema.newValidator();
FileInputStream fileInputStream2 = new FileInputStream(new File("Input.xml"));
val.validate(new StreamSource(fileInputStream2));

关于java - 使用 validator 针对 xsd 验证soap xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292015/

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