相关文章推荐
另类的单车  ·  【原创】SQLSERVER 通过 ...·  2 年前    · 
文武双全的企鹅  ·  JavaScript reduce() ...·  3 年前    · 
失眠的番茄  ·  机器学习有很多关于核函数的说法,核函数的定义 ...·  3 年前    · 
伤情的感冒药  ·  How to convert ...·  3 年前    · 
买醉的硬币  ·  用tailwindcss的思想+scss手撸 ...·  3 年前    · 
Code  ›  nodejs解析xml文件开发者社区
var nodejs学习 xml语言
https://cloud.tencent.com/developer/article/1653903
傻傻的香烟
3 年前
作者头像
OECOM
0 篇文章

nodejs解析xml文件

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > OECOM > nodejs解析xml文件

nodejs解析xml文件

作者头像
OECOM
发布 于 2020-07-01 17:23:58
3.8K 0
发布 于 2020-07-01 17:23:58
举报

xml作为一种重要的数据交换格式,在使用nodejs中也需要对其进行解析,这里所使用的解析模块是xmlreader。其作用是将xml根据节点解析为一个JSON对象,里面包含了一系列的方法,下面来看具体的使用方法。

首先是安装xmlreader模块

npm install xmlreader -S

然后就是引用了

var express = require('express');
var router = express.Router();
var http = require('http');
var https = require('https');
var readXml = require('xmlreader');
/* GET users listing. */
router.get('/readSitemap', function(req, res) {
var options = {
    hostname: 'www.oecom.cn',       
    path: '/sitemap.xml',    
    method: 'GET'    
var clientReq = https.request(options, function (clientRes) { 
var xmlData = '';  
var xmlURLArray = []; 
    console.log('STATUS: ' + clientRes.statusCode);    
    console.log('HEADERS: ' + JSON.stringify(clientRes.headers));    
    clientRes.setEncoding('utf8');    
    clientRes.on('data', function (chunk) {    
        //console.log('BODY: ' + chunk);
        xmlData+=chunk;
    clientRes.on('end', function (chunk) {
    	readXml.read(xmlData, function(errors, xmlResponse){  
		    if(null !== errors ){  
		        console.log(errors)  
		        return;  
		    console.log(xmlResponse);
		    xmlResponse.urlset.url.array.map(item=>{
		    	 xmlURLArray.push(item.loc.text());
		    res.end(JSON.stringify(xmlResponse.urlset.url.array[0]));
 
推荐文章
另类的单车  ·  【原创】SQLSERVER 通过 newsequentialid() 获取有序Guid 的注意事项 - 深海澜鲸 - 博客园
2 年前
文武双全的企鹅  ·  JavaScript reduce() 方法 | 菜鸟教程
3 年前
失眠的番茄  ·  机器学习有很多关于核函数的说法,核函数的定义和作用是什么? - 知乎
3 年前
伤情的感冒药  ·  How to convert k4a_image_t to opencv matrix? (Azure Kinect Sensor SDK) - Stack Overflow
3 年前
买醉的硬币  ·  用tailwindcss的思想+scss手撸公有样式 - 掘金
3 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号