Load xml file "weak"-schema-aware

Post here questions and problems related to oXygen frameworks/document types.
Patrik
Posts: 280
Joined: Thu Nov 28, 2013 9:32 am
Location: Hamburg/Germany
Contact:

Load xml file "weak"-schema-aware

Post by Patrik »

Hi,

I'm using ParserCreator.newSchemaAwareDocumentBuilder() to load the complete book in my custom framework from java. Now it occurs quite often, that a small (and easily recoverable) bug in the xml code (non-unique id, missing included file, missing mandatory element, ...) keeps the file from being loaded. Since I hardly need the default attributes to be added I can't use a non-schema-aware document builder as well.

Since oXygen can handle these document pretty well when opening them in author mode, I'm wondering if there is a way to load such files with expanded default attributes but without validation from within my java extension as well.

Thanks and regards,

Patrik
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Load xml file "weak"-schema-aware

Post by alex_jitianu »

Hello Patrik,

It appears that the default error handler is the one that when notified, decides to trow an exception. You can set an error handler that just ignores the received notifications:

Code: Select all


DocumentBuilder newDocumentBuilder = ParserCreator.newSchemaAwareDocumentBuilder();
newDocumentBuilder.setErrorHandler(new NoErrorsReportedHandler());
where the handler looks like this:

Code: Select all

  public class NoErrorsReportedHandler implements XMLErrorHandler, ErrorHandler {
@Override
public void warning(String p0, String p1, XMLParseException ex) throws XNIException {
// Consumes warnings.
}
@Override
public void error(String p0, String p1, XMLParseException ex) throws XNIException {
// Consumes errors.
}
public void fatalError(String p0, String p1, XMLParseException ex) throws XNIException {
}
@Override
public void warning(SAXParseException ex) throws SAXException {
}
@Override
public void error(SAXParseException ex) throws SAXException {
}
@Override
public void fatalError(SAXParseException ex) throws SAXException {
}
}

Best regards,
Alex
Patrik
Posts: 280
Joined: Thu Nov 28, 2013 9:32 am
Location: Hamburg/Germany
Contact:

Re: Load xml file "weak"-schema-aware

Post by Patrik »

Hi Alex,

thanks for the code. It works nicely for the common validation errors (missing elements, dublicate ids).

But a failed xinclude still aborts loading the file. Is there maybe a way to keep the xincludes from being resolved at all? I could easily do this in my own java code.

BTW: The class ParserCreator is not listed in the javadoc. Is this on purpose or accidentally?

Thanks and Regards,
Patrik
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Load xml file "weak"-schema-aware

Post by alex_jitianu »

Hi Patrik,

If not expanding external entities is also a desired effect you can do it like this:

Code: Select all


    boolean fakeResolver = false;
boolean noExpand = true;
boolean namespaceAware = true;
URL schemaUrl = null;
boolean schemaAware = true;
boolean forceXIncludeAndBaseURIFixup = false;
boolean dynamicValidation = true;

parserCreator.newDocumentBuilder(fakeResolver, noExpand, namespaceAware, schemaUrl, schemaAware, forceXIncludeAndBaseURIFixup, dynamicValidation);

Initially we didn't considered ParserCreator to be API per se.But as the need for it has risen we let it in the public code. Starting with 17.0 it will also be present in the Javadoc.
Otherwise, you can disable just XInclude processing by setting this option before creating the document builder:

Code: Select all

Options.getInstance().setBooleanProperty("validation.xinclude", false)
Just don't forget to set it back to true afterwards.

Best regards,
Alex
Patrik
Posts: 280
Joined: Thu Nov 28, 2013 9:32 am
Location: Hamburg/Germany
Contact:

Re: Load xml file "weak"-schema-aware

Post by Patrik »

Thanks for the documentation. It worked but I realized that I need the to resolve the xincludes. However, adding an empty fallback in all xincludes is a sufficient alternative.

Regards,
Patrik
Post Reply