Dovresti secondo me implementare in modo opportuno l'handler del
validatore, se ce la fai posta il risultato.
Sarebbe interessante individuare con xPath il punto preciso che genera
l'errore di validazione (nel caso di xml well-formed ovviamente).
Ti do un hint postandoti il codice che uso io con un handler
personalizzato, per validare uso JAXP.
Gli import sono:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
Il codice che valida è:
// VALIDA L'XML USANDO JAXP
String rqXSDPath = "/mio_schema.xsd";
org.w3c.dom.Document docW3C = null;
try{
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
factory.setAttribute("
http://java.sun.com/xml/jaxp/properties/
schemaLanguage",
"
http://www.w3.org/2001/XMLSchema" );
factory.setAttribute("
http://java.sun.com/xml/jaxp/properties/
schemaSource", rqXSDPath);
DocumentBuilder builder = factory.newDocumentBuilder();
Validator handler = new Validator();
builder.setErrorHandler(handler);
docW3C = builder.parse(inBuffered);
if(handler.validationError == true) {
handler.saxParseException.getMessage();
}
} catch(java.io.IOException ioe) {
ioe.printStackTrace();
}
catch (SAXException e) {
e.printStackTrace();
}
catch (ParserConfigurationException e) {
e.printStackTrace();
} }
La classe nested Validator personalizzata richiamata nel codice sopra
è:
private class Validator extends DefaultHandler {
public boolean validationError =false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws
SAXException {
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{}}