sidd
sidd

Reputation: 305

Validating an XML against an XSD using Apache Camel

My aim is to read a CSV file, convert it to an XML and validate it against an XSD. Below is the code:

    CamelContext _ctx = new DefaultCamelContext(); 
    _ctx.addRoutes(new RouteBuilder() {

        public void configure() throws Exception {
            from("file:src/main/resources?fileName=data-sample.csv")
            .process(new MyTransformValidator())
            .to("file:src/main/resources/?fileName=emp.xml")
            .to("validator:src/main/resources?fileName=SampleXMLStructure.xsd");                
        }
});

Error:

Exception in thread "main" org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> To[validator:src/main/resources?fileName=SampleXMLStructure.xsd] <<< in route: Route(route1)[From[file:src/main/resources?fileName=data-sam... because of Failed to resolve endpoint: validator://src/main/resources?fileName=SampleXMLStructure.xsd due to: Failed to resolve endpoint: validator://src/main/resources?fileName=SampleXMLStructure.xsd due to: There are 1 parameters that couldn't be set on the endpoint. Check the uri if the parameters are spelt correctly and that they are properties of the endpoint. Unknown parameters=[{fileName=SampleXMLStructure.xsd}]

Also,I would like to configure if some Exception occurs if the XML is not valid for the given XSD. How do we configure this?

Please kindly help.

Upvotes: 1

Views: 1877

Answers (1)

c0ld
c0ld

Reputation: 855

With exception block try like this:

from("file:src/main/resources?fileName=data-sample.csv")
            .process(new MyTransformValidator())
            .to("file:src/main/resources/?fileName=emp.xml")
            .doTry()
            .to("validator:file:src/main/resources/SampleXMLStructure.xsd")
            .doCatch(Exception.class)
            .log(LoggingLevel.WARN, "Failed validation cause:${exchangeProperty.CamelExceptionCaught}")
            .process(exchange -> {
                Throwable exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
                // process exception
            })
            .end()
            .log(LoggingLevel.INFO, "Finished processing file"); 

Upvotes: 0

Related Questions