Java 解析 XML 字符串并提取 Channel 标签数据为 JSON 格式
使用 Java 中的 DOM 解析器可以处理字符串格式的 XML 信息,并使用 JSON 库将 Channel 标签里面的数据转化为 JSON 格式。
以下示例代码展示了如何将 XML 字符串中的 Channel 标签数据提取并转化为 JSON 格式:
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
public class XmlToJsonExample {
public static void main(String[] args) {
String xmlString = '<Organization><Department></Department><Devices><Device><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 {
// 创建DOM解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建DOM解析器
DocumentBuilder builder = factory.newDocumentBuilder();
// 将XML字符串转化为输入流
ByteArrayInputStream input = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
// 解析XML文档并获取Document对象
Document document = builder.parse(input);
// 获取Device元素
NodeList deviceList = document.getElementsByTagName("Device");
// 获取第一个Device元素
Element deviceElement = (Element) deviceList.item(0);
// 获取UnitNodes元素
NodeList unitNodesList = deviceElement.getElementsByTagName("UnitNodes");
// 获取第一个UnitNodes元素
Element unitNodesElement = (Element) unitNodesList.item(0);
// 获取Channel元素列表
NodeList channelList = unitNodesElement.getElementsByTagName("Channel");
// 创建JSONArray对象,用于存储转化后的JSON数据
JSONArray jsonArray = new JSONArray();
// 遍历Channel元素列表
for (int i = 0; i < channelList.getLength(); i++) {
// 获取Channel元素
Element channelElement = (Element) channelList.item(i);
// 创建JSONObject对象,用于存储Channel数据
JSONObject json = new JSONObject();
// 获取Channel元素的属性值
String id = channelElement.getAttribute("id");
String name = channelElement.getAttribute("name");
String desc = channelElement.getAttribute("desc");
String status = channelElement.getAttribute("status");
String channelType = channelElement.getAttribute("channelType");
String channelSN = channelElement.getAttribute("channelSN");
String rights = channelElement.getAttribute("rights");
String alarmType = channelElement.getAttribute("alarmType");
String alarmLevel = channelElement.getAttribute("alarmLevel");
// 将属性值存储到JSONObject对象中
json.put("id", id);
json.put("name", name);
json.put("desc", desc);
json.put("status", status);
json.put("channelType", channelType);
json.put("channelSN", channelSN);
json.put("rights", rights);
json.put("alarmType", alarmType);
json.put("alarmLevel", alarmLevel);
// 将JSONObject对象添加到JSONArray中
jsonArray.put(json);
}
// 将JSONArray对象转化为字符串输出
String jsonString = jsonArray.toString();
System.out.println(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行上述代码,输出结果为:
[{"channelType":"0","status":"0","rights":"11000000000000000000000011101000000101100011111111111111111","desc":"","alarmType":"80081","id":"1000028$3$0$0","name":"防区1","alarmLevel":"1","channelSN":""}]
以上代码使用 DOM 解析器将 XML 字符串解析为 Document 对象,然后根据 XML 结构获取到 Channel 元素的属性值,并将其存储到 JSONObject 对象中,最后将 JSONObject 对象添加到 JSONArray 中。最终将 JSONArray 对象转化为字符串输出。
原文地址: https://www.cveoy.top/t/topic/fRaf 著作权归作者所有。请勿转载和采集!