如何使用Tika的XWPFWordExtractorDecorator类? [英] How to use Tika's XWPFWordExtractorDecorator class?

查看:469
本文介绍了如何使用Tika的XWPFWordExtractorDecorator类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我,Tika的XWPFWordExtractorDecorator类用于DOCX转换成HTML。但我不知道如何使用这个类来获得DOCX的HTML。任何其他库做相同的工作也是AP preciated /

Someone told me that Tika's XWPFWordExtractorDecorator class is used to convert docx into html. But I am not sure how to use this class to get the HTML from docx. Any other library for doing the same job is also appreciated/

推荐答案

您不应该直接使用它

相反,调用蒂卡通常的方式,它会调用适当的code你

Instead, call Tika in the usual way, and it'll call the appropriate code for you

如果您从解析文件要XHTML中,code看起来像

If you want XHTML from parsing a file, the code looks something like

    // Either of these will work, the latter is recommended
    //InputStream input = new FileInputStream("test.docx");
    InputStream input = TikaInputStream.get(new File("test.docx"));

    // AutoDetect is normally best, unless you know the best parser for the type
    Parser parser = new AutoDetectParser();

    // Handler for indented XHTML
    StringWriter sw = new StringWriter();
    SAXTransformerFactory factory = (SAXTransformerFactory)
             SAXTransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
    handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(new StreamResult(sw));

    // Call the Tika Parser
    try {
        Metadata metadata = new Metadata();
        parser.parse(input, handler, metadata, new ParseContext());
        String xml = sw.toString();
    } finally {
        input.close();
    }

这篇关于如何使用Tika的XWPFWordExtractorDecorator类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆