Java service class to validate XML string against an XSD file

Java service to validate the XML against an XSD file present in a location without creating the IS document type on IS.

Input: xmlData, xsdPath

Output: isValid
             errorMessage

 
 
import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
import com.wm.util.ServerException;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
 
public final class validateXML_SVC
 
{
 
/** 
* The primary method for the Java service
*
* @param pipeline
*            The IData pipeline
* @throws ServiceException
*/
public static final void validateXML(IData pipeline) throws ServiceException {
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String xmlData = IDataUtil.getString( pipelineCursor, "xmlData" );
String xsdPath = IDataUtil.getString( pipelineCursor, "xsdPath" );
String errorMessage="";
Boolean isValid=true;
 
 
try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File(xsdPath));
        Validator validator = schema.newValidator();
        StringReader reader = new StringReader(xmlData);
        validator.validate(new StreamSource(reader));
    } catch (IOException | SAXException e) {
        System.out.println("Exception: "+e.getMessage());
        errorMessage = e.toString();
        isValid=false;      
    }
 
IDataUtil.put( pipelineCursor, "isValid", isValid);
IDataUtil.put( pipelineCursor, "errorMessage", errorMessage);
pipelineCursor.destroy();
}
 
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
 
 
 
// --- <<IS-END-SHARED-SOURCE-AREA>> ---
 
final static validateXML_SVC _instance = new validateXML_SVC();
 
static validateXML_SVC _newInstance() { return new validateXML_SVC(); }
 
static validateXML_SVC _cast(Object o) { return (validateXML_SVC)o; }
 
}
 
 
Results:
 
Scenario 1:
 
xmlData= 

<?xml version="1.0"?>

<note xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com note.xsd">

<to>a</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

xsdPath=C:\\SoftwareAG103\\IntegrationServer\\instances\\default\\note.xsd

isValid=false

errorMessage=org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 11; cvc-datatype-valid.1.2.1: 'a' is not a valid value for 'integer'.

Scenario 2:

xmlData= 

<?xml version="1.0"?>

<note xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com note.xsd">

<to>1</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

xsdPath=C:\\SoftwareAG103\\IntegrationServer\\instances\\default\\note.xsd

isValid=true

Feel free to post comments or email if you have any questions.

Cheers,

Mahesh K Sreenivas

Senior Integration Consultant and Architect