JavaScript解析XML字符串为JSON

JavaScript解析XML字符串为JSON

JavaScript使用ObjTree.js解析xml文件为JSON对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?xml version="1.0" encoding="utf-8"?>
<Organization>
<Department coding="001" name="根节点" modifytime="1436940546" MaxDepID="0" sn="" memo="" deptype="0" depsort="0" isPlatform="0" chargebooth="0">
<Device id="1000019001" type="65537" name="海康106" manufacturer="2" ip="127.0.0.2" prot="61001" unitnum="1" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000018001" type="65548" name="海康106" manufacturer="1" ip="127.0.0.2" prot="61001" unitnum="2" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000017001" type="65548" name="海康106" manufacturer="1" ip="127.0.0.2" prot="61001" unitnum="3" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000016001" type="65548" name="海康106" manufacturer="1" ip="127.0.0.2" prot="61001" unitnum="4" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000015001" type="65548" name="海康106" manufacturer="2" ip="127.0.0.2" prot="61001" unitnum="5" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
</Department>
<Department coding="001" name="根节点" modifytime="1436940546" MaxDepID="0" sn="" memo="" deptype="0" depsort="0" isPlatform="0" chargebooth="0">
<Device id="1000019001" type="65537" name="海康106" manufacturer="2" ip="127.0.0.2" prot="61001" unitnum="1" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000018001" type="65548" name="海康106" manufacturer="1" ip="127.0.0.2" prot="61001" unitnum="2" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000017001" type="65548" name="海康106" manufacturer="1" ip="127.0.0.2" prot="61001" unitnum="3" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000016001" type="65548" name="海康106" manufacturer="1" ip="127.0.0.2" prot="61001" unitnum="4" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
<Device id="1000015001" type="65548" name="海康106" manufacturer="2" ip="127.0.0.2" prot="61001" unitnum="5" status="2" rights="110000111001111">
<UnitNodes index="0" channelnum="48" streamType="81" type="1" subType="0" zeroChnEncode="0">
<Channel id="10000978001@001$1$0$0" name="GB49_1-" desc="" channelType="1" channelSN="10005"></Channel>
<Channel id="10000978001@001$1$0$1" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
<Channel id="10000978001@001$1$0$2" name="GB49_1-" desc="" channelType="1" channelSN="10005" ></Channel>
</UnitNodes>
</Device>
</Department>
</Organization>
  • 解析方法

组装自定义的JSON格式数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function parseXMLData(root) {
console.info('----------------------------解析组织树数据----------------------------');
var rootTree = [];
for (let i = 0; i < root.length; i++) {
// console.info(root[i]);
let id = root[i]['-coding']; // 组织树编号
let title = root[i]['-name']; // 组织树名称(对应为根节点名称)
let device = root[i].Device; //设备树
let children = [];
for (let i = 0; i < device.length; i++) {
let device_id = device[i]['-id'];
let device_ip = device[i]['-ip'];
let device_manufacturer = device[i]['-manufacturer'];
let device_name = device[i]['-name'];
let device_port = device[i]['-port'];
let device_type = device[i]['-type'];

let device_channels = device[i].UnitNodes.Channel;

let channels = [];
for (let i = 0; i < device_channels.length; i++) {
// console.info(device_channels[i]);
let channel_id = device_channels[i]['-id'];
let channel_name = device_channels[i]['-name'];
let channel_sn = device_channels[i]['-channelSN'];
let channel_type = device_channels[i]['-channelType'];

channels.push({
"id": channel_id,
"title": channel_name,
"channelSN": channel_sn,
"channelType": channel_type,
"isLeaf": true
});
}
children.push({
"id": device_id,
"title": device_name,
"children": channels,
"ip": device_ip,
"port": device_port,
"type": device_type,
"manufacturer": device_manufacturer
})

}
rootTree.push({
"id": id,
"title": title,
"children": children
});
}
//console.info(rootTree);
return rootTree;
}