xml字符串保存xml文件或xml转换String字符串

Myth丶恋晨 2022-04-12 13:20 615阅读 0赞

废话不多说,直接上主题,先来一个xml字符串保存xml文件的方法。

  1. /**
  2. * 字符串转xml文件并保存指定路径
  3. * @param xmlStr xml字符串
  4. * @param fileName 文件名称
  5. * @param type 文件类型(后缀)
  6. * @param path (存放路径)
  7. * @return
  8. */
  9. @SuppressWarnings("deprecation")
  10. public static String createXml(String xmlStr,String fileName,String type,String path) {
  11. Document doc = strToDocument(xmlStr);
  12. String realPath = path + fileName + type;
  13. try {
  14. // 判断文件是否存在,如存在就删掉它
  15. File file = new File(realPath);
  16. if (!file.getParentFile().exists()) {
  17. //如果不存在则创建
  18. file.getParentFile().mkdirs();
  19. logger.info("==============文件目录不存在,新建文件==============");
  20. }
  21. /** 将document中的内容写入文件中 */
  22. TransformerFactory tFactory = TransformerFactory.newInstance();
  23. Transformer transformer = tFactory.newTransformer();
  24. transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  25. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  26. DOMSource source = new DOMSource(doc);
  27. StreamResult result = new StreamResult(new FileOutputStream(realPath));
  28. transformer.transform(source, result);
  29. } catch (final Exception exception) {
  30. logger.info("更新" + fileName + "出错:"+exception);
  31. }
  32. //返回文件保存路径
  33. return path + fileName + type;
  34. }
  35. /**
  36. * 字符串转Document
  37. * @param xmlStr
  38. * @return
  39. */
  40. public static Document strToDocument(String xmlStr) {
  41. Document doc = null;
  42. StringReader sr = new StringReader(xmlStr);
  43. InputSource is = new InputSource(sr);
  44. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  45. DocumentBuilder builder;
  46. try {
  47. builder = factory.newDocumentBuilder();
  48. doc = builder.parse(is);
  49. } catch (ParserConfigurationException e) {
  50. logger.info("ParserConfiguration错误"+e);
  51. } catch (SAXException e) {
  52. logger.info("SAX错误"+e);
  53. } catch (IOException e) {
  54. logger.info("IO错误"+e);
  55. }
  56. return doc;
  57. }

不明白请仔细看注释,可以直接复制粘贴使用;

接下来是根据路径将xml文件转换为xml字符串

  1. /**
  2. * xml文件转换字符串
  3. * @param path
  4. * @return
  5. */
  6. public static String XmlFileToStr(String path){
  7. String xmlString = "";
  8. byte[] strBuffer = null;
  9. InputStream in = null;
  10. int flen = 0;
  11. File xmlfile = new File(path);
  12. try {
  13. in = new FileInputStream(xmlfile);
  14. flen = (int)xmlfile.length();
  15. strBuffer = new byte[flen];
  16. in.read(strBuffer, 0, flen);
  17. in.close();
  18. } catch (FileNotFoundException e) {
  19. e.printStackTrace();
  20. logger.info("FileNotFound错误"+e.getMessage());
  21. } catch (IOException e) {
  22. logger.info("转换IO错误"+e.getMessage());
  23. e.printStackTrace();
  24. }
  25. xmlString = new String(strBuffer); //构建String时,可用byte[]类型,
  26. logger.info("xml文件转换后的字符串:"+xmlString);
  27. return xmlString;
  28. }

相信大家可以看懂,其实很简单。有问题请留言告知。希望对大家有点帮助!

发表评论

表情:
评论列表 (有 0 条评论,615人围观)

还没有评论,来说两句吧...

相关阅读