xml 和json互转工具- 工具

时间:2025-02-16 13:36:03

有个问题会导致springboot返回捕获的异常编程xml格式

 

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.json.JSONObject;
import org.json.XML;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class XmlJsonConverter {

    private static final ObjectMapper objectMapper = new ObjectMapper(); // JSON处理
    private static final XmlMapper xmlMapper = new XmlMapper(); // XML处理

    // JSON转XML
    public static String jsonToXml(String json) throws Exception {
        // 将JSON转换为Map
        Object jsonObj = objectMapper.readValue(json, Object.class);
        // 将Map转换为XML
        return xmlMapper.writeValueAsString(jsonObj);
    }

    // XML转JSON
    public static String xmlToJson(String xml) throws Exception {
        // 将XML转换为Map
        Object xmlObj = xmlMapper.readValue(xml, Object.class);
        // 将Map转换为JSON
        return objectMapper.writeValueAsString(xmlObj);
    }

    // 将XML字符串转换为XML文档对象
    private static Document toXmlDocument(String xml) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        return builder.parse(inputStream);
    }

    // 将XML文档对象转换为JSON字符串
    public static String xmlDocumentToJson(Document doc) throws Exception {
        // XML文档转换为Map对象
        Object xmlObj = xmlMapper.readValue(xmlMapper.writeValueAsString(doc), Object.class);
        // Map转换为JSON
        return objectMapper.writeValueAsString(xmlObj);
    }

   
    // 测试代码
    public static void main (String[] args) {
        try {
            // JSON 转 XML
            String json = "{\"name\":\"John\", \"age\":30}";
            String xml = jsonToXml(json);
            System.out.println("JSON to XML: \n" + xml);

            // XML 转 JSON
            String xmlStr = "<person><name>John</name><age>30</age></person>";
            String jsonResult = xmlToJson(xmlStr);
            System.out.println("XML to JSON: \n" + jsonResult);
 
}

 <!-- Jackson XML dependency -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.15.0</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20210307</version>
        </dependency>

        <!-- Jackson Core dependency -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.15.0</version>
        </dependency>

        <!-- Jackson Databind dependency -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.0</version>
        </dependency>