Java Dom4j 解析 XML 字符串并转换为 JSON 格式 - 附代码示例
Java Dom4j 解析 XML 字符串并转换为 JSON 格式
本文将介绍如何使用 Java 的 Dom4j 库解析字符串格式的 XML 信息,并以 XML 中 Channel 标签的数据为例,将其转换为 JSON 格式。
需求
假设我们有以下 XML 信息:
<Organization>
<Department></Department>
<Devices>
<Device></Device>
<Device id='1000028' type='601' name='报警主机'>
<UnitNodes index='0' channelnum='60' type='3'>
<Channel id='1000028$3$0$0' name='防区1' desc='' status='0' channelType='0' channelSN='' rights='11000000000000000000000011101000000101100011111111111111111' alarmType='80081' alarmLevel='1' />
</UnitNodes>
</Device>
</Devices>
</Organization>
我们需要提取最后一个 <Device> 标签内的所有 <Channel> 标签数据,并将其转换为 JSON 格式。
代码示例
以下是使用 Dom4j 和 JSON 库实现该需求的 Java 代码:
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
public class XmlToJsonConverter {
public static void main(String[] args) {
String xml = '<Organization><Department></Department><Devices><Device></Device><Device id='1000028' type='601' name='报警主机'><UnitNodes index='0' channelnum='60' type='3'><Channel id='1000028$3$0$0' name='防区1' desc='' status='0' channelType='0' channelSN='' rights='11000000000000000000000011101000000101100011111111111111111' alarmType='80081' alarmLevel='1' /></UnitNodes></Device> </Devices></Organization>';
try {
Document document = new SAXReader().read(new ByteArrayInputStream(xml.getBytes()));
Element rootElement = document.getRootElement();
Element deviceElement = (Element) rootElement.selectSingleNode("//Device[last()]");
JSONObject json = new JSONObject();
json.put("id", deviceElement.attributeValue("id"));
json.put("type", deviceElement.attributeValue("type"));
json.put("name", deviceElement.attributeValue("name"));
JSONArray channels = new JSONArray();
Element channelElement = (Element) deviceElement.selectSingleNode(".//Channel");
while (channelElement != null) {
JSONObject channel = new JSONObject();
channel.put("id", channelElement.attributeValue("id"));
channel.put("name", channelElement.attributeValue("name"));
channel.put("desc", channelElement.attributeValue("desc"));
channel.put("status", channelElement.attributeValue("status"));
channel.put("channelType", channelElement.attributeValue("channelType"));
channel.put("channelSN", channelElement.attributeValue("channelSN"));
channel.put("rights", channelElement.attributeValue("rights"));
channel.put("alarmType", channelElement.attributeValue("alarmType"));
channel.put("alarmLevel", channelElement.attributeValue("alarmLevel"));
channels.put(channel);
channelElement = (Element) channelElement.selectSingleNode("following-sibling::Channel");
}
json.put("channels", channels);
System.out.println(json.toString(2)); // 使用 toString(2) 格式化输出 JSON
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
解释
- 引入必要的库: 代码首先引入了
org.dom4j和org.json库,用于处理 XML 和 JSON 数据。 - 解析 XML 字符串: 使用
SAXReader将 XML 字符串解析为Document对象。 - 定位目标节点: 使用 XPath 定位到最后一个
<Device>元素,并获取其属性值。 - 提取 Channel 数据: 遍历
<Device>元素下的所有<Channel>元素,并提取其属性值存储到JSONObject中。 - 构建 JSON 数组: 将所有
<Channel>数据存储到JSONArray中。 - 组装最终 JSON: 将
<Device>信息和<Channel>数组组装成最终的 JSON 对象。 - 输出 JSON: 将最终的 JSON 对象打印到控制台。
输出结果
{
"id": "1000028",
"type": "601",
"name": "报警主机",
"channels": [
{
"id": "1000028$3$0$0",
"name": "防区1",
"desc": "",
"status": "0",
"channelType": "0",
"channelSN": "",
"rights": "11000000000000000000000011101000000101100011111111111111111",
"alarmType": "80081",
"alarmLevel": "1"
}
]
}
总结
本文介绍了如何使用 Java Dom4j 库解析 XML 字符串并将指定标签数据转换为 JSON 格式,并提供了详细的代码示例。您可以根据自己的需求修改代码,提取不同的 XML 节点信息并进行相应的 JSON 格式转换。
原文地址: https://www.cveoy.top/t/topic/fRbg 著作权归作者所有。请勿转载和采集!