xml和json的转化,复杂的xml如何转化json对象,如何获取到Item中的每个属性的数据
xml数据如下:
<PatrolDevice>
<SendCode>Client01SendCode>
<ReceiveCode>Server01ReceiveCode>
<Type>2Type>
<Code/>
<Command/>
<Time>2022-01-01 12:02:34Time>
<Items>
<Item patroldevice_name ="机器人1" patroldevice_code="Robot01"
time="2022-01-01 12:02:12" type ="1" value="0.4" value_unit="0.4m/s" unit="m/s"/>
<Item patroldevice_name ="机器人2" patroldevice_code ="Robot01"
time="2022-01-01 12:02:12" type ="2" value="942.5" value_unit="942.5m" unit="m"/>
<Item patroldevice_name ="机器人3" patroldevice_code ="Robot01"
time="2022-01-01 12:02:12" type ="3" value="90.5" value_unit="90.5%" unit="%"/>
Items>
想要得到的数据如下:
{
"PatrolDevice": {
"SendCode": "Client01",
"ReceiveCode": "Server01",
"Type": "2",
"Time": "2022-01-01 12:02:34",
"Items": {
"Item": [
{
"-patroldevice_name": "500kV机器人",
"-patroldevice_code": "Robot01",
"-time": "2022-01-01 12:02:12",
"-type": "1",
"-value": "0.4",
"-value_unit": "0.4m/s",
"-unit": "m/s"
},
{
"-patroldevice_name": "500kV机器人",
"-patroldevice_code": "Robot01",
"-time": "2022-01-01 12:02:12",
"-type": "2",
"-value": "942.5",
"-value_unit": "942.5m",
"-unit": "m"
},
{
"-patroldevice_name": "500kV机器人",
"-patroldevice_code": "Robot01",
"-time": "2022-01-01 12:02:12",
"-type": "3",
"-value": "90.5",
"-value_unit": "90.5%",
"-unit": "%"
}
]
}
}
}
xML是一种通用的数据交换格式,它的平台无关性、语言无关性、系统无关性、给数据集成与交互带来了极大的方便。 XML在不同的语言环境中解析方式都是一样的,只不过实现的语法不同而已。
XML的解析方式分为四种:
1、DOM解析;
2、SAX解析;
3、JDOM解析;
4、DOM4J解析。
其中前两种属于基础方法,是官方提供的平台无关的解析方式;后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台