XmlUtil.java 849 B

12345678910111213141516171819202122232425262728
  1. package org.ruoyi.util;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.dataformat.xml.XmlMapper;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. /**
  8. * @author https://www.wdbyte.com
  9. */
  10. @Slf4j
  11. public class XmlUtil {
  12. public static String xml2json(String requestBody) {
  13. requestBody = StringUtils.trim(requestBody);
  14. XmlMapper xmlMapper = new XmlMapper();
  15. JsonNode node = null;
  16. try {
  17. node = xmlMapper.readTree(requestBody.getBytes());
  18. ObjectMapper jsonMapper = new ObjectMapper();
  19. return jsonMapper.writeValueAsString(node);
  20. } catch (Exception e) {
  21. log.error("xml 2 json error,msg:" + e.getMessage(), e);
  22. }
  23. return null;
  24. }
  25. }