Re: Validate XML against Schema
- From: "robert" <robertlazarski@xxxxxxxxx>
- Date: 6 Mar 2006 12:45:32 -0800
Paco escreveu:
I'm trying to validate some XML against a schema. The code I have
works but it won't give me any useful information for where the error
is in the XML. I can get useful information (line numbers) if there
are problems when loading the schema and XML. However, the exceptions
generated from validate() always have line and column numbers of "-1".
Am I doing something wrong or should I be going about this in a totally
different method? My code is below (minus error handling and such).
The SAXErrorHandler is a class I created that simply prints out the
message and line and column numbers.
// Load Schema
File schemaFile = new File("schema.xsd");
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(schemaFile);
Schema schema = factory.newSchema(schemaSource);
// Load XML
File xmlFile = new File("data.xml");
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = parser.parse(xmlFile);
// Validate
Validator validator = schema.newValidator();
SAXErrorHandler errHandler = new SAXErrorHandler();
validator.setErrorHandler(errHandler);
DOMResult result = new DOMResult();
validator.validate(new DOMSource(document), result);
You could define your own Handler that extends DefaultHandler to get
into error details - you should have more luck than -1. You don't show
your class but implementing DefaultHandler is a good place to start.
The point being that there are other handlers - see the sax2 o'reilly
book or google for more info. The entityResolver shown is for locally
resolving URI's and is probably not relevant:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
DocumentBuilder db = dbf.newDocumentBuilder();
//Add a custom error handler
MyDefaultHandler dh = new MyDefaultHandler();
db.setErrorHandler(dh);
db.setEntityResolver(new SchemaLoader());
InputSource isXml = new InputSource (new StringReader(xml));
Document doc_in = db.parse(isXml);
if (false == dh.isSchemaValidated)
{
throw new java.lang.IllegalStateException("ERR: Invalid XML
received - schema validation failed");
}
//Custom error hanler to print errors and return isValid
class MyDefaultHandler extends DefaultHandler
{
//flag to check if the xml document was valid
public boolean isSchemaValidated = true;
//Receive notification of a recoverable error.
public void error(SAXParseException se)
{
setValidity(se);
}
//Receive notification of a non-recoverable error.
public void fatalError(SAXParseException se)
{
setValidity(se);
}
//Receive notification of a warning.
public void warning(SAXParseException se)
{
setValidity(se);
}
private void setValidity(SAXParseException se)
{
isSchemaValidated = false;
Fwlog.error(this, Fwlog.DB, se);
}
}
Now if your SAXParseException trace shows -1, than I advice with going
with a different jar implementation in the classloader.
HTH,
Robert
http://www.braziloutsource.com/
.
- Follow-Ups:
- Re: Validate XML against Schema
- From: Paco
- Re: Validate XML against Schema
- References:
- Validate XML against Schema
- From: Paco
- Validate XML against Schema
- Prev by Date: Calling sun.tools.javac from Applet
- Next by Date: Re: Eclipse bug?
- Previous by thread: Validate XML against Schema
- Next by thread: Re: Validate XML against Schema
- Index(es):
Relevant Pages
|