java - Can I get additional information about the error from SAXParseException and XMLStreamReader -
i using default javax.api.validator validating xml file against xsd i.e saxparser
schemafactory factory = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri); schema schema = factory.newschema(new file(schemapath)); streamsource xml = new streamsource(new file(xmlpath)); xmlstreamreader reader = xmlinputfactory.newfactory().createxmlstreamreader(xml); validator validator = schema.newvalidator(); validator.seterrorhandler(new basicerrorhandler(reader)); validator.validate(new staxsource(reader));
here errorhandler:
public class basicerrorhandler implements errorhandler { private xmlstreamreader reader; public basicerrorhandler(xmlstreamreader reader) { this.reader = reader; } @override public void error(saxparseexception e) throws saxexception { warning(e); } @override public void fatalerror(saxparseexception e) throws saxexception { warning(e); } @override public void warning(saxparseexception e) throws saxexception { system.out.println(reader.getlocalname()); e.printstacktrace(system.out); } }
i can node invalid calling reader.getlocalname() tried methods on reader haven't been able value causes exception. e.g id have tag
<startdate>asdfasdf</startdate>
which date field according schema (xsd) code above can node i.e can find out startdate invalid field. value invalid not able deduce that.
has worked or faced similar situation.
i've tested code , gives full error message including value that's invalid.
the error
method of basicerrorhandler
called twice , holds saxparseexception
following messages:
cvc-datatype-valid.1.2.1: 'asdfasdf' not valid value 'date'.
and
cvc-type.3.1.3: value 'asdfasdf' of element 'startdate' not valid.
it's not possible invalid value string
. because reader
's position beyond text element (event type xmlstreamconstants.end_element
) when error triggered , sax parses forward only.
Comments
Post a Comment