java 处理字符串格式的xml信息并给出代码和示例 xml信息为OrganizationDepartmentDepartmentDevicesDeviceDeviceDevicesOrganization获取其中Device id=1000028 type=601 name=报警主机UnitNodes index=0 channelnum=60 type=3Channel id=100002
可以使用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></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/ieKA 著作权归作者所有。请勿转载和采集!