3

I need to parse an XML file to DOM. I also want to validate this against an XSD schema, and be able to use a transformer to convert this with an XSL style sheet. While this worked fine as a plain Java application, I have had serious problems trying to get this to work on Android.

  1. First, I had to discover that the Android DOM parser is hard-coded to refuse validation. This is why I decided to try using Xerces instead of the native Android XML support.
  2. To get Xerces to run on Android, I understand a repackaged version of it is needed to avoid conflicts with the java core libary. So I followed the suggestion repeated by several contributors to use the Xerces-for-Android library on Google Code. Note: Since Google Code is closed, it now has been ported to the (currently same) Xerces-for-Android library on Github.
  3. As pointed out there, this library can cause NoClassDefFoundError with various classes. In my application, I ran into this problem with

    • DocumentBuilderFactory.newInstance() (which needs DocumentBuilderFactoryImpl).
    • TransformerFactory.newInstance() (which needs TransformerFactoryImpl).

Is there any advice how to make Xerces-for-Android work with these?

Community
  • 1
  • 1
urps
  • 366
  • 1
  • 4
  • 13

1 Answers1

1

For the problem with DocumentBuilderFactoryImpl, since the missing class is part of the Xerces-for-Android package, just not automatically found, there seem to be two possible fixes:

  1. As suggested here, one might try rebuilding the jar with updated settings that allows the class to be automatically found.
  2. Since newInstance() also provides an overloaded version which allows to directly specify the missing package, I found the easier solution was to simply enter the path manually:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
                      ("mf.org.apache.xerces.jaxp.DocumentBuilderFactoryImpl",null);
    

For TransformerFactoryImpl, things seem more complicated. The default setting points to a native Java class, com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl, for which I could not find any equivalent included in Xerces-for-Android. So unless I want to try extracting and repackaging the Java TransformerFactoryImpl class and all other native classes this relies on, I am stuck here.

So at this point, I see the following solution options:

  • either to forgo the entire schema validation and stick with the Android implementation (for which a working version of the TransformerFactoryImpl exists, but which has other shortcomings, for example, no DOMImplementationLS for serialization),
  • or, if the validation is vital, still use the Android implementation for almost everything, just use Xerces-for-Android for a self-contained validation step,
  • or use Xerces-for-Android for almost everything but for the XSL transformation. For this, I seem to need to output the Xerces DOM to stream/cache files first, then apply the transformation with the Android Transformer, then parse the result stream back to the DOM representation of Xerces.

All of these seem either lacking features or provide clumsy workarounds, so any advice on a fully working Xerces-for-Android library or different options is welcome.

Community
  • 1
  • 1
urps
  • 366
  • 1
  • 4
  • 13