uniapp解析XML
使用npm安装xmldom初始化npmnpminit安装xmldomnpmixmldom引入xmldom,解析xml//解析本地xml地
使用npm安装xmldom
- 初始化npm
npm init
安装 xmldom
npm i xmldom
下载完成
引入xmldom,解析xml
// 解析本地xml uni.request({ url: 'http://localhost:8080/static/Area.xml', header: { 'content-type': 'application/xml' }, success: function(res) { // 直接引入下载完成目录 var DOMParser = require('../../../wxcomponents/xmldom').DOMParser; var XMLParser = new DOMParser(); var doc = XMLParser.parseFromString(res.data); console.log(doc) var countylist = doc.getElementsByTagName('county'); var county_arr = new Array; var street_arr = new Array; for (var i in countylist){ var county = countylist[i]; if (typeof county === 'object' && typeof county.getAttribute === 'function'){ var name = county.getAttribute('name') var code = county.getAttribute('code') var street = county.getElementsByTagName('street') var data = { 'name': name, 'code': code, street: new Array}; if (typeof county === 'object') { for (var ii in street){ if (typeof street[ii] == 'object' && typeof street[ii].getAttribute === 'function'){ var name = street[ii].getAttribute('name') var code = street[ii].getAttribute('code') // street_arr.push({ 'name': name, 'code': code }) data.street.push({ 'name': name, 'code': code }); } } } county_arr.push(data) } } console.log(county_arr) // console.log(street_arr) } });
相关文章
- okhttp、okhttp3使用post方式发送form-data数据
- JAVA字符串逗号分隔并对每个字符串添加引号
- [Docker系列] Install BaoTa with Docker
- [信创系列]银河麒麟安装nodejs18和npm2,并启动对应工程
- EasyExcel导出Excel并合并单元格
- Python3安装pip及pip安装whl包
- [Ubuntu系列]Ubuntu 安装 Harbor
- [Ubuntu系列]Ubuntu 安装 docker 及修改 docker 存储位置
- [Centos系列]CentOS 修改 DNS
- [Centos系列]source /etc/profile 无法永久生效问题
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~