在 Java 中将 XML 转换为 JSON 有几种常见的方式,我将为您介绍其中三种方式,并提供每种方式的详细步骤流程以及相应的代码示例。
步骤流程:
Maven 依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.5</version>
</dependency>
Gradle 依赖:
implementation 'com.fasterxml.jackson.core:jackson-core:2.12.5'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'
示例代码:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlToJsonUsingJackson {
public static void main(String[] args) throws Exception {
String xml = "<root><name>John</name><age>30</age></root>";
ObjectMapper xmlMapper = new XmlMapper();
JsonNode jsonNode = xmlMapper.readTree(xml);
ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(jsonNode);
System.out.println(json);
}
}
步骤流程:
Maven 依赖:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
Gradle 依赖:
implementation 'org.json:json:20210307'
示例代码:
import org.json.JSONObject;
import org.json.XML;
public class XmlToJsonUsingOrgJson {
public static void main(String[] args) {
String xml = "<root><name>John</name><age>30</age></root>";
JSONObject jsonObject = XML.toJSONObject(xml);
String json = jsonObject.toString(4); // 4 is the indentation level
System.out.println(json);
}
}
步骤流程:
示例代码:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.StringReader;
import org.json.JSONObject;
public class XmlToJsonUsingDOM {
public static void main(String[] args) throws Exception {
String xml = "<root><name>John</name><age>30</age></root>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
JSONObject json = convertXmlToJson(document.getDocumentElement());
String jsonString = json.toString(4); // 4 is the indentation level
System.out.println(jsonString);
}
private static JSONObject convertXmlToJson(Node node) {
JSONObject json = new JSONObject();
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NamedNodeMap attributes = element.getAttributes();
if (attributes.getLength() > 0) {
JSONObject attributeJson = new JSONObject();
for (int i = 0; i < attributes.getLength(); i++) {
Attr attribute = (Attr) attributes.item(i);
attributeJson.put(attribute.getName(), attribute.getValue());
}
json.put("attributes", attributeJson);
}
}
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
if (child.getChildNodes().getLength() == 1 && child.getFirstChild().getNodeType() == Node.TEXT_NODE) {
json.put(child.getNodeName(), child.getTextContent());
} else {
if (!json.has(child.getNodeName())) {
json.put(child.getNodeName(), new JSONArray());
}
((JSONArray) json.get(child.getNodeName())).put(convertXmlToJson(child));
}
}
}
return json;
}
}
请注意,这些示例代码中的 XML 和 JSON 结构仅为演示目的,实际使用时需要根据您的需求进行适当的调整。每种方式都有其优缺点,根据具体情况选择合适的方法。