加载xml文件

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.web.multipart.MultipartFile;

import com.bonc.usdp.odk.common.file.FileUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MyXmlUtil {
   /**
    * 加载xml文件
    */
   public static Document load(MultipartFile file) throws Exception {
      Document document = null;
      File convFile = null;
      convFile = new File(file.getOriginalFilename());
      convFile.createNewFile();
      FileOutputStream fos = new FileOutputStream(convFile);
      fos.write(file.getBytes());
      fos.flush();
      fos.close();
      SAXBuilder reader = new SAXBuilder();
      document = reader.build(convFile);
      return document;
   }
   
   public static Document load(File file) throws Exception {
      Document document = null;
      SAXBuilder reader = new SAXBuilder();
      document = reader.build(file);
      return document;
   }

   /**
    * 将MultipartFile 类型的xml文件转换为String串
    */
   public static Map<String, Object> XmlToMap(MultipartFile file) throws Exception {
      Map<String, Object> rsMap = new HashMap<>();
      Document document = null;
      document = load(file);

      Format format = Format.getPrettyFormat();
      format.setEncoding("UTF-8");// 设置编码格式

      StringWriter out = null; // 输出对象
      String textFromFile = ""; // 输出字符串
      XMLOutputter outputter = new XMLOutputter();
      out = new StringWriter();
      outputter.output(document, out);
      textFromFile = out.toString();
      JSONObject xmlJSONObj = XML.toJSONObject(textFromFile);
      String jsonPrettyPrintString = xmlJSONObj.toString();
      ObjectMapper mapper = new ObjectMapper();
      rsMap = mapper.readValue(jsonPrettyPrintString, new TypeReference<Map<String, Object>>() {
      });

      File fileF = null;
      try {
         fileF = File.createTempFile("tmp", null);
         file.transferTo(fileF);
         fileF.deleteOnExit();
      } catch (IOException e) {
         e.printStackTrace();
      }
      FileUtil.deleteFile(fileF);
      return rsMap;
   }

   public static Map<String, Object> XmlToMap(File file) throws Exception {
      Map<String, Object> rsMap = new HashMap<>();
      Document document = null;
      document = load(file);

      Format format = Format.getPrettyFormat();
      format.setEncoding("UTF-8");// 设置编码格式

      StringWriter out = null; // 输出对象
      String textFromFile = ""; // 输出字符串
      XMLOutputter outputter = new XMLOutputter();
      out = new StringWriter();
      outputter.output(document, out);
      textFromFile = out.toString();
      JSONObject xmlJSONObj = XML.toJSONObject(textFromFile);
      String jsonPrettyPrintString = xmlJSONObj.toString();
      ObjectMapper mapper = new ObjectMapper();
      rsMap = mapper.readValue(jsonPrettyPrintString, new TypeReference<Map<String, Object>>() {
      });

      return rsMap;
   }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值