JavaScript解析XML字符串为JSON

本文介绍如何使用ObjTree.js插件将复杂的XML文件转换为JSON对象。通过详细解析示例XML文件,展示了一种方法来组装自定义JSON格式的数据,特别关注于组织树和设备信息的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

<?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格式数据

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值