Hello!
It looks like there might be an bug in the :
parser.setProperty("
http://java.sun.com/xml/jaxp/properties/schemaSource", inputSource);
I ended up in a lot of different errors. For eg
1) I just got half of the file from the end?!!!
2) Premature end of file (I propably the other half)
3) Strange charachters
It looks like I needed to read the xsd in some other way.
With some test back and fore I ended up with:
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);
//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);
//Use this when file is outside jar
//parser.setProperty("
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", xsdFilePath);
java.io.InputStream is = getClass().getClassLoader().getResourceAsStream(xsdFilePath);
//Seems to be a bug with
//parser.setProperty("
http://java.sun.com/xml/jaxp/properties/schemaSource", inputSource);
//There for we must read the xsd like
java.io.InputStreamReader inr = new java.io.InputStreamReader(is);
java.io.BufferedReader buf = new java.io.BufferedReader(inr);
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while( (line = buf.readLine()) != null )
{
stringBuffer.append(line);
}
is.close();
inr.close();
buf.close();
org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(new java.io.StringReader(stringBuffer.toString()));
parser.setProperty("
http://java.sun.com/xml/jaxp/properties/schemaSource", inputSource);
//Parse xml against xsd
parser.setErrorHandler(this);
parser.parse(new InputSource(new StringReader(tempXml)));
if (saxParseException == null) {
return true;
} else {
return false;
}
} catch (Exception e) {
logger.error(e, e);
return false;
}
}
Please comment of you see anything stupid.
But it looks like we must re-read the xsd.
BTW Please give a "svar" so I can reward you for your time and valuable help!
/Fredrik