2

I am newby of XML.

I using Apache Commons XMLConfiguration for my configuration.

I need a schema based validation.

The xsd is locateded in the resources folder of my java project.

The xml and xsd are not located in the same location.

Following the documentation (that report only an example with DTD). I produce the following code:

    URL urlXsd = getClass().getResource("config.xsd");
    DefaultEntityResolver resolver = new DefaultEntityResolver();
    resolver.registerEntityId("configuration", urlXsd);

    XMLConfiguration xmlConfig = new XMLConfiguration();
    xmlConfig.setEntityResolver(resolver);
    xmlConfig.setSchemaValidation(true);

     xmlConfig.load(new File("config.xml"));

I get following exception:

Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; cvc-elt.1: Cannot find the declaration of element 'configuration'.

My config.xsd:

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
         <xs:element name="configuration">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="reader">
                           </xs:element>
                           <xs:element name="writer">
                           </xs:element>
                     </xs:sequence>
               </xs:complexType>
         </xs:element>
   </xs:schema>

My config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
   <reader>
   </reader>
   <writer>
   </writer>
</configuration>

Can anyone help me?

theShadow89
  • 1,307
  • 20
  • 44

1 Answers1

0

I followed the documentation as well (also in the current Apache Commons Configuration2) and the loading of the schema doesn't work in this case. At the bottom of the documentation it states:

The mechanisms provided with Commons Configuration will hopefully be sufficient in most cases, however there will certainly be circumstances where they are not. XMLConfiguration provides two extension mechanisms that should provide applications with all the flexibility they may need. The first, registering a custom Entity Resolver has already been discussed in the preceeding section. The second is that XMLConfiguration provides a generic way of setting up the XML parser to use: A preconfigured DocumentBuilder object can be passed to the setDocumentBuilder() method.

I suggest you use "setDocumentBuilder", instead of "setEntityResolver" like this:

   Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File("C:\\config.xsd"));
   DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
   docBuilderFactory.setSchema(schema);
   DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

   //if you want an exception to be thrown when there is invalid xml document,
   //you need to set your own ErrorHandler because the default
   //behavior is to just print an error message.
   docBuilder.setErrorHandler(new ErrorHandler() {
       @Override
       public void warning(SAXParseException exception) throws SAXException {
           throw exception;
       }

       @Override
       public void error(SAXParseException exception) throws SAXException {
           throw exception;
       }

       @Override
       public void fatalError(SAXParseException exception)  throws SAXException {
           throw exception;
       }  
   });

   conf = new BasicConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml()
           .setFileName("config.xml")
           .setDocumentBuilder(docBuilder)
           .setSchemaValidation(true)              
           .setExpressionEngine(new XPathExpressionEngine())               
           ).getConfiguration();
   FileHandler fh = new FileHandler(conf);
   fh.load(xmlStream);
user2209562
  • 244
  • 2
  • 16