`
javabkb
  • 浏览: 53426 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

将word转化为xml

阅读更多
   刚到公司头儿就给我布置了任务,可以将word里的内容读取出来,持久化到数据库中。一开始觉得,用流读一下不就行了吗?后来发现没有这么简单。Word的.doc格式是不公开的,所以Java很难直接支持读.doc文件,网上的提供的信息也不多。有一个通过poi的组件来读取的,但是通过poi来读取excel的内容很方便,读取word实在是不给力了,具体原因apache官网也说了在poi中处理word模块的作者离开apache组织了(http://poi.apache.org/hwpf/index.html)。现在主要是面临三个问题:第一,支持中文;第二,保证word03和word07兼容;第三,对图片表格等格式的内容都能取读出来。请教高人后得知,打开word后可以直接另存为xml的格式,只要能得到xml的格式,就可以通过Dom4j解析得到内容。但是如何通过程序将word转化成xml呢?
   网上查了一下,免费的主要有两种方法:
   1,通过jacob来实现(http://danadler.com/jacob/).jacob是在java与微软的com组件之间的桥梁,通过使用jacob自带的dll动态链接库通过jni的方式实现了在sun java平台上的程序对com调用.但是这种方式未能解决word07转化为Xml的问题,所以只能弃之。
   2,通过jodconveter来实现转化(http://www.artofsolving.com/opensource/jodconverter)。这种方式实现起来比较麻烦,操作有点繁琐,但是能解决全部上述问题。jobconveter团队的才智表示敬佩:通过启动OpenOffice.org的服务端口,实现程序操作opeanoffice实现文件的转换。
   具体的实现方式在http://nopainnogain.iteye.com/blog/819432 此文中已有详细描述.

   另外贴上我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;


public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
        test t = new test();
        //File in = new File("d:\\mytest\\test1.pdf");
        //File out = new File("d:\\mytest\\test11.html");
        FileInputStream input = new FileInputStream("d:\\mytest\\test11.pdf");
        FileOutputStream output = new FileOutputStream("d:\\mytest\\test11.doc");

        t.convert(input, output);
	}
	
	public void convert(File input, File output) throws Exception

    {

        OpenOfficeConnection conn = new SocketOpenOfficeConnection("localhost", 8100);

        conn.connect();

        DocumentConverter converter = new OpenOfficeDocumentConverter(conn);

        converter.convert(input, output);

        conn.disconnect();

    }
	
    public void convert(InputStream input, OutputStream output) throws Exception

    {

        OpenOfficeConnection conn = new SocketOpenOfficeConnection("localhost", 8100);

        conn.connect();

        DocumentConverter converter = new OpenOfficeDocumentConverter(conn);

        DocumentFormatRegistry registry = new DefaultDocumentFormatRegistry();

        converter.convert(input, registry.getFormatByFileExtension("pdf"), output, registry.getFormatByFileExtension("doc"));

        conn.disconnect();

    }
}

附件里有jodconverter的zip包,可以在这里直接下了,openoffice的安装文件就不贴了,到官网下就行(http://www.openoffice.org/).

1
0
分享到:
评论
2 楼 lijun169 2016-07-11  
坑货,XML呢
1 楼 fetianlong 2014-03-31  
请问,你怎么转成XML的?

相关推荐

Global site tag (gtag.js) - Google Analytics