How to validate a xml with a xsd?
Hello!I'm developing a EJB app. The idea is that the EJB should validate a xml-stucture with a xsd.
This was working fine until I needed to put it all in a jar-file.
The structure of the ejb.jar looks like:
com/myapp
META-INF
lib
resources/xmlskeletons
resourses/xsd
I populate a document build upon a xmlskeleton.
Then I would like to validate the xml-structure according to the xsd.
Before, when this was in a exploaded file structure, I could set the xsd-path as a element in the xml-structure.
But now when the xsd is inside the jar, and as you can see in the code below, the xsd-file will not be found.
So if you have any suggestions how to include the xsd for the validation from a jar please let me know!!
BTW I can just use Java 1.4
Best regards
Fredrik
public boolean validateXML(String xml, String xsdFilePath, String nameSpace, String rootElementName)
{
try {
//Build temporary document
Document documentTemp = buildDocumentFromString(xml);
//Set namespace and schemaLocation for validation this will not concern the paths in xml sent to IC
setXmlElementAttribute(rootElementName, "xmlns", nameSpace, documentTemp);
setXmlElementAttribute(rootElementName, "xsi:schemaLocation",
nameSpace + " " + System.getProperty("user.dir") + "/" + xsdFilePath, documentTemp);
//Transform document to xml String
String tempXml = getXmlStringFromDocument(documentTemp);
//Validate the xml String
saxParseException = null;
SAXParser parser = new SAXParser();
//parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", xsdFilePath);
parser.setErrorHandler(this);
parser.parse(new InputSource(new StringReader(tempXml)));
if (saxParseException == null) {
return true;
} else {
return false;
}
} catch (Exception e) {
printExceptionToFile("XML_EXCECPTION", e);
return false;
}
}