我在google中搜索过,但是没有一个帖子符合我的要求,因此请按照我的要求进行发布。我的意图是针对架构验证xml。
我具有如下所示的父子xsd层次结构,
a.xsd(导入b.xsd)
<?xml version="1.0"?>
<xsd:schema xmlns="http://service/parentnamesapce.com" xmlns:child="http://service/childnamesapce.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://service/parentnamesapce.com"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import schemaLocation="b.xsd" namespace="http://service/childnamesapce.com"/>
<xsd:element name="APPReq" type="child:APPReq_Type">
</xsd:element>
<xsd:element name="GetDocument">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="APPReq">
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
b.xsd
<?xml version="1.0"?>
<xsd:schema xmlns="http://service/childnamesapce.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://service/childnamesapce.com"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:element name="Header" type="Header_Type">
<xsd:element name="Data" type="Data_Type">
<xsd:element name="HeaderName">
<xsd:simpleType>
<xsd:restriction base="A"/>
</xsd:simpleType>
</xsd:element>
<xsd:simpleType name="A">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
<xsd:complexType name="APPReq_Type">
<xsd:sequence>
<xsd:element ref="Header"/>
<xsd:element ref="Data"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Header_Type">
<xsd:sequence>
<xsd:element ref="HeaderName"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
// **Similar elements for Data as well**
request.xml(来自客户端)
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ns0:APPReq xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns0="http://service/parentnamesapce.com"
xmlns:ns1="http://service/childnamesapce.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<ns1:Header>
<ns1:HeaderName>ID</ns1:HeaderName>
</ns1:Header>
<ns1:Data>
<ns1:DataValue>Hello</ns1:DataValue>
</ns1:Data>
</ns0:APPReq>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Java 8代码段:-
pulic class Validator{
public boolean validateXML(String xml){
try {
SchemaFactory sf= SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
sf.setResourceResolver(new MyResourceResolver()); // this is to load and parse the child xsd
Source srcFile= new StreamSource(getClass.getClassLoader().getResourceAsStream("a.xsd"));
Schema schema = sf.newSchema(srcFile);
Validator validator = schema.newValidator();
validator.validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
}catch(Exception e) {//to do}
}
问题:-
执行代码时出现以下错误
**org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'SOAP-ENV:Envelope'**
有人能告诉我我在做什么错吗?
有两种可能。
一个是找不到您的架构。这可能是因为MyResourceResolver
有错误。您没有向我们显示代码,所以我们不能告诉。
另一个原因是该消息表示其内容:它找到了架构,并且该架构不包含元素SOAP-ENV:Envelope
的声明。如果它确实包含这样的声明,那么您将其隐藏得很好。
通过“我在google中搜索但没有帖子满足我的需求”的方式,建议您使用错误的方法。您需要了解XSD的概念。当遇到错误消息时,尝试一下然后再进行谷歌搜索就永远无法使事情正常进行。您可能会收到该错误消息,原因有100种,但其中大多数都等于您在开始编码之前没有做足够的阅读工作。