目标:将单词doc的内容读入cosole
使用的环境:Windows 7 32位 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.
答案 0 :(得分:1)
披露:我处理docx4j项目
您需要使用Android-specific branch of docx4j。
请参阅jaxb-can-be-made-to-run-on-android
我建议你让AndroidDocxToHtml先行,然后继续前进。
答案 1 :(得分:0)
答案是指IOException“标记已失效”: 我正在使用docx4j的Android分支,并在尝试加载模板DOCX时遇到同样的错误。我改为以下面的方式加载docx文件,这解决了我的问题:
final LoadFromZipNG loader = new LoadFromZipNG();
mypackage= (WordprocessingMLPackage)loader.get(is);
取自DOCX4J样本:https://github.com/plutext/AndroidDocxToHtml