2

目的:将word doc的内容读到cosole上

使用环境:Windows 7 32bit Eclipse IDE Indigo Android 4.1 API 16 Java 1.6 库:docx4j-2.8.0

以下是我用来在控​​制台上打印 word doc 文本的代码片段

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(Environment.getExternalStorageDirectory().getAbsolutePath(), selectedFromList));

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); 
System.out.println("file contents --->>>>>" +documentPart.getContent());    

DocPropsCorePart docPropsCorePart = wordMLPackage.getDocPropsCorePart();
CoreProperties coreProps = (CoreProperties)docPropsCorePart.getJaxbElement();

System.out.println("'dc:title' is " + coreProps.getTitle().getValue().getContent().get(0));
System.out.println(coreProps.getTitle().getValue().getClass().getName() );
System.out.println("'dcterms:created' is " + coreProps.getCreated().getClass().getName() );

当我尝试执行上述代码时,出现以下问题 eclipse logcat 显示以下日志

08-10 12:22:37.853: I/dalvikvm(1761): Could not find method javax.xml.bind.JAXBContext.createMarshaller, referenced from method 

org.docx4j.openpackaging.packages.WordprocessingMLPackage.save
08-10 12:22:37.853: W/dalvikvm(1761): VFY: unable to resolve virtual method 7579: Ljavax/xml/bind/JAXBContext;.createMarshaller ()Ljavax/xml/bind/Marshaller;
08-10 12:22:37.853: D/dalvikvm(1761): VFY: replacing opcode 0x6e at 0x0017

08-10 12:22:37.863: I/dalvikvm(1761): Could not find method javax.xml.bind.JAXBContext.createMarshaller, referenced from method 

org.docx4j.openpackaging.packages.WordprocessingMLPackage.transform

08-10 12:22:37.863: W/dalvikvm(1761): VFY: unable to resolve virtual method 7579: Ljavax/xml/bind/JAXBContext;.createMarshaller ()Ljavax/xml/bind/Marshaller;
08-10 12:22:37.863: D/dalvikvm(1761): VFY: replacing opcode 0x6e at 0x000d

08-10 12:22:37.863: I/dalvikvm(1761): Failed resolving Lorg/docx4j/jaxb/JaxbValidationEventHandler; interface 1254 'Ljavax/xml/bind/ValidationEventHandler;'
08-10 12:22:37.863: W/dalvikvm(1761): Link of class 'Lorg/docx4j/jaxb/JaxbValidationEventHandler;' failed

08-10 12:22:37.873: D/dalvikvm(1761): DexOpt: unable to opt direct call 0x46db at 0x1e in Lorg/docx4j/openpackaging/packages/WordprocessingMLPackage;.transform
08-10 12:22:37.873: D/dalvikvm(1761): DexOpt: unable to opt direct call 0x1dba at 0x26 in Lorg/docx4j/openpackaging/packages/WordprocessingMLPackage;.transform

08-10 12:22:37.883: E/dalvikvm(1761): Could not find class 'org.apache.poi.poifs.filesystem.POIFSFileSystem', referenced from method 

org.docx4j.openpackaging.packages.OpcPackage.load

08-10 12:22:37.883: W/dalvikvm(1761): VFY: unable to resolve new-instance 1658 (Lorg/apache/poi/poifs/filesystem/POIFSFileSystem;) in 

Lorg/docx4j/openpackaging/packages/OpcPackage;
08-10 12:22:37.883: D/dalvikvm(1761): VFY: replacing opcode 0x22 at 0x001a

08-10 12:22:37.893: I/dalvikvm(1761): Could not find method javax.xml.bind.JAXBContext.createUnmarshaller, referenced from method 

org.docx4j.openpackaging.packages.OpcPackage.load

08-10 12:22:37.893: W/dalvikvm(1761): VFY: unable to resolve virtual method 7580: Ljavax/xml/bind/JAXBContext;.createUnmarshaller ()Ljavax/xml/bind/Unmarshaller;
08-10 12:22:37.893: D/dalvikvm(1761): VFY: replacing opcode 0x6e at 0x004d

08-10 12:22:37.893: I/dalvikvm(1761): Could not find method javax.xml.bind.JAXBContext.createMarshaller, referenced from method 

org.docx4j.openpackaging.packages.OpcPackage.save

08-10 12:22:37.893: W/dalvikvm(1761): VFY: unable to resolve virtual method 7579: Ljavax/xml/bind/JAXBContext;.createMarshaller ()Ljavax/xml/bind/Marshaller;
08-10 12:22:37.893: D/dalvikvm(1761): VFY: replacing opcode 0x6e at 0x0017

08-10 12:22:37.905: D/dalvikvm(1761): DexOpt: unable to opt direct call 0x2654 at 0x1c in Lorg/docx4j/openpackaging/packages/OpcPackage;.load
08-10 12:22:37.905: D/dalvikvm(1761): DexOpt: unable to opt direct call 0x2651 at 0x21 in Lorg/docx4j/openpackaging/packages/OpcPackage;.load

08-10 12:22:37.905: I/dalvikvm(1761): Failed resolving Lorg/docx4j/jaxb/JaxbValidationEventHandler; interface 1254 'Ljavax/xml/bind/ValidationEventHandler;'
08-10 12:22:37.905: W/dalvikvm(1761): Link of class 'Lorg/docx4j/jaxb/JaxbValidationEventHandler;' failed

08-10 12:22:37.905: D/dalvikvm(1761): DexOpt: unable to opt direct call 0x46db at 0x53 in Lorg/docx4j/openpackaging/packages/OpcPackage;.load
08-10 12:22:37.963: W/System.err(1761): org.docx4j.openpackaging.exceptions.Docx4JException: Error reading from the stream

08-10 12:22:37.963: W/System.err(1761):     at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:239)
08-10 12:22:37.963: W/System.err(1761):     at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:195)
08-10 12:22:37.963: W/System.err(1761):     at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:178)
08-10 12:22:37.963: W/System.err(1761):     at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:172)
08-10 12:22:37.975: W/System.err(1761):     at com.reader.MainActivity$2.onItemClick(MainActivity.java:125)
08-10 12:22:37.975: W/System.err(1761):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-10 12:22:37.975: W/System.err(1761):     at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
08-10 12:22:37.975: W/System.err(1761):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
08-10 12:22:37.975: W/System.err(1761):     at android.widget.AbsListView$1.run(AbsListView.java:3529)
08-10 12:22:37.984: W/System.err(1761):     at android.os.Handler.handleCallback(Handler.java:615)
08-10 12:22:37.984: W/System.err(1761):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-10 12:22:37.984: W/System.err(1761):     at android.os.Looper.loop(Looper.java:137)
08-10 12:22:37.984: W/System.err(1761):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-10 12:22:37.984: W/System.err(1761):     at java.lang.reflect.Method.invokeNative(Native Method)
08-10 12:22:37.993: W/System.err(1761):     at java.lang.reflect.Method.invoke(Method.java:511)
08-10 12:22:37.993: W/System.err(1761):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-10 12:22:37.993: W/System.err(1761):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-10 12:22:37.993: W/System.err(1761):     at dalvik.system.NativeStart.main(Native Method)
08-10 12:22:38.085: W/System.err(1761): Caused by: java.io.IOException: Mark has been invalidated.
08-10 12:22:38.085: W/System.err(1761):     at java.io.BufferedInputStream.reset(BufferedInputStream.java:350)
08-10 12:22:38.093: W/System.err(1761):     at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:237)

08-10 12:22:38.923: I/Choreographer(1761): Skipped 107 frames!  The application may be doing too much work on its main thread.
4

2 回答 2

2

披露:我从事 docx4j 项目

您需要使用docx4j 的 Android 特定分支

请参阅jaxb-can-be-made-to-run-on-android

我建议你先让 AndroidDocxToHtml工作,然后从那里继续。

于 2012-08-11T00:20:56.230 回答
0

答案是指 IOException "Mark has been invalidated":我正在使用 docx4j 的 Android 分支,在尝试加载模板 DOCX 时遇到相同的错误。我更改为通过以下方式加载 docx 文件,从而解决了我的问题:

        final LoadFromZipNG loader = new LoadFromZipNG();
        mypackage= (WordprocessingMLPackage)loader.get(is);

取自 DOCX4J-Sample:https ://github.com/plutext/AndroidDocxToHtml

于 2012-12-09T10:49:36.830 回答