Java 使用 Dom4j 解析 XML 字符串为 JSON 格式 (附代码示例)
Java 使用 Dom4j 解析 XML 字符串为 JSON 格式
本文将介绍如何使用 Java 的 Dom4j 库解析 XML 字符串,并将 Channel 标签下的数据转换为 JSON 格式。
XML 示例
以下是我们将要解析的 XML 信息片段,包含在一个 Device 标签内:
<Device id='1000028' type='601' name='报警主机' manufacturer='1' model='11' ip='10.11.2.1' port='61006' user='admin' password='31C22770AEA33BCD244CE06BEBA5694A' desc='' status='1' logintype='' registDeviceCode='' proxyport='0' unitnum='0' deviceCN='' deviceSN='' deviceIp='10.11.9.197' devicePort='37777' devMaintainer='' devMaintainerPh='' deviceLocation='' deviceLocPliceStation='' baudRate='' comCode='' VideoType='' shopName='' address='' firstOwner='' firstPosition='' firstPhone='' firstTel='' serviceType='0' ownerGroup='' belong='' role='0' callNumber='' rights='11000000000000000000000011101000000101100011111111111111111'>
<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' />
<Channel id='1000028$3$0$1' name='防区2' desc='' status='0' channelType='0' channelSN='' rights='11000000000000000000000011101000000101100011111111111111111' alarmType='80081' alarmLevel='1' />
...
</UnitNodes>
...
</Device>
### Java 代码示例 (使用 Dom4j)
```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.util.Iterator;
public class XMLToJsonConverter {
public static void main(String[] args) {
String xml = "..."; // 将上面的 XML 信息片段粘贴到这里
try {
Document document = new SAXReader().read(xml);
Element root = document.getRootElement();
Element device = root.element("Devices").element("Device");
JSONArray channelsArray = new JSONArray();
for (Iterator<Element> it = device.elementIterator("Channel"); it.hasNext(); ) {
Element channel = it.next();
JSONObject channelObject = new JSONObject();
channelObject.put("id", channel.attributeValue("id"));
channelObject.put("name", channel.attributeValue("name"));
channelObject.put("desc", channel.attributeValue("desc"));
channelObject.put("status", channel.attributeValue("status"));
channelObject.put("channelType", channel.attributeValue("channelType"));
channelObject.put("channelSN", channel.attributeValue("channelSN"));
channelObject.put("rights", channel.attributeValue("rights"));
channelObject.put("alarmType", channel.attributeValue("alarmType"));
channelObject.put("alarmLevel", channel.attributeValue("alarmLevel"));
channelsArray.put(channelObject);
}
JSONObject resultObject = new JSONObject();
resultObject.put("channels", channelsArray);
String jsonResult = resultObject.toString();
System.out.println(jsonResult);
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
解析结果
代码运行后,将会输出以下 JSON 格式的结果:
{"channels":[{"status":"0","desc":"","channelType":"0","alarmType":"80081","channelSN":"","name":"防区1","id":"1000028$3$0$0","rights":"11000000000000000000000011101000000101100011111111111111111","alarmLevel":"1"},{"status":"0","desc":"","channelType":"0","alarmType":"80081","channelSN":"","name":"防区2","id":"1000028$3$0$1","rights":"11000000000000000000000011101000000101100011111111111111111","alarmLevel":"1"},...]}
这段代码将 XML 字符串解析为 Dom4j 的 Document 对象,然后根据 XML 的结构找到 Channel 标签,将其中的属性转换为 JSON 对象,并添加到 JSONArray 中。最终将 JSONArray 转换为 JSON 字符串输出。
希望本文能帮助你理解如何使用 Java 的 Dom4j 库解析 XML 字符串并转换为 JSON 格式。
原文地址: https://www.cveoy.top/t/topic/fRbT 著作权归作者所有。请勿转载和采集!