简体   繁体   English

Java 通过导入验证 xml 针对 xsd

[英]Java Validate xml against xsd with import

I searched in google but none of the post have what I m looking for, so posting it with my requirement.我在谷歌搜索,但没有一个帖子有我要找的东西,所以按照我的要求发布。 My intention is validating xml against the schema.我的意图是针对架构验证 xml。

I have parent child xsd hierarchy like below,我有父子 xsd 层次结构,如下所示,

a.xsd (imports b.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 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 (which comes from client) 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 Code Snippet:- 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}
}

Problem:-问题:-

When I execute the code I am getting the following error当我执行代码时,出现以下错误

 **org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'SOAP-ENV:Envelope'**

Could any one tell me what i am doing wrong here?谁能告诉我我在这里做错了什么?

There are two possibilities.有两种可能性。

One is that it didn't find your schema.一是它没有找到您的架构。 That could be because MyResourceResolver has a bug.那可能是因为MyResourceResolver有一个错误。 You haven't shown us the code, so we can't tell.您还没有向我们展示代码,所以我们无法判断。

The other reason is that the message means what it says: it found the schema and the schema doesn't contain a declaration for element SOAP-ENV:Envelope .另一个原因是消息的意思是它所说的:它找到了架构并且架构不包含元素SOAP-ENV:Envelope的声明。 If it does contain such a declaration, then you've kept it well hidden.如果它确实包含这样的声明,那么您已经很好地隐藏了它。

By the way "I searched in google but none of the post have what I'm looking for" suggests that you are going about this the wrong way.顺便说一句,“我在谷歌搜索,但没有一个帖子有我要找的东西”表明你正在以错误的方式解决这个问题。 You need to understand the concepts of XSD.您需要了解 XSD 的概念。 You'll never get things working by trying things out and then googling when you hit an error message.通过尝试并在遇到错误消息时进行谷歌搜索,您将永远无法使事情正常进行。 There are 100 reasons you could get that error message, but most of them amount to the fact that you didn't do enough reading before you started coding.收到错误消息的原因有 100 个,但其中大多数原因是您在开始编码之前没有做足够的阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM