简体   繁体   中英

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.

I have parent child xsd hierarchy like below,

a.xsd (imports 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 (which comes from client)

    <?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:-

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. 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 . 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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