将word、pdf等转为xml文件

word转xml

示例:  

        // 1、创建document对象
        Document document =     DocumentHelper.createDocument();
        // 2、创建根节点rss
        Element rss = document.addElement("归档数据"); 
        //  3、读取文件
        file = new File(filePath + "/test/" + contentaccsavename);        
        //4、将file转为byte字节并追加到document下
        byte[] bytes = new byte[0];
        String fileBase64new = "";
        byte[] bytesByFile = getBytesByFile(file);
        fileBase64new = Base64.encodeToString(bytesByFile);
        Element bmsj = rss .addElement("编码数据");
        bmsj.setText(fileBase64new);
        //5、 创建一个空的文件xml在当月文件夹中
        OutputFormat format = OutputFormat.createPrettyPrint();
        // 设置编码格式
        format.setEncoding("UTF-8");
        FileWriter fileWriter = null;
        try {
                 
             File file = new File(filePath +"/test/" + year + month);
              if (file.exists() == false) {
                 file.mkdirs();
              }
             fileWriter = new FileWriter(filePath+"/test/" + year + month + "/" + reqId + ".xml");
         } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
         }
            // 文件大小
         int size = 0;
         try {
              // 将xml写入到文件中
              XMLWriter xmlWriter = new XMLWriter(fileWriter, format);
              // 设置是否转义,默认使用转义字符
              xmlWriter.setEscapeText(false);
              xmlWriter.write(document);
              xmlWriter.close();  
              File file = new File(filePath + "/test/" + year + month + "/" + reqId + ".xml");
         } catch (IOException e) {
                // TODO Auto-generated catch block
             e.printStackTrace();
         }            

 

 

posted @ 2023-07-24 09:50  向上丶  阅读(720)  评论(0编辑  收藏  举报