ItsAmy
ItsAmy

Reputation: 844

Convert docx to html in Android

I would like to convert a Word docx document (not doc) to html in Android. I tried using Apache XWPF, but it uses native libraries in javax.xml.stream, which are not available in Android. When I try to manually include them in my build.gradle file with the line compile 'javax.xml.stream:stax-api:1.0-2', Android studio gives me this very descriptive error about using native libraries:

trouble processing "javax/xml/stream/EventFilter.class":
Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.
This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

<Information about how to override this warning and why it's a bad idea>

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

When I don't manually include those libraries, I get this exception at runtime:

 java.lang.ClassNotFoundException: Didn't find class "javax.xml.stream.XMLEventFactory" on path: DexPathList[[zip file "/data/app/<package>"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:41)
            at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:162)
            at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:142)
            at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)
            at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:89)
            at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:273)
            at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
            at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:123)

When running the following code:

XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(wordFileName));

Where wordFileName is a valid path to a word document.

So my question is this: Is it possible to use Apache XWPF on Android, and if not, what can I use instead to convert docx files to html?

Upvotes: 1

Views: 2533

Answers (1)

ItsAmy
ItsAmy

Reputation: 844

I was not able to get Apache XWPF working, but I was able to use Docx4j (sample code for Android here), which worked for my purposes. I just had to include the libraries found in that project.

Upvotes: 1

Related Questions