相关文章推荐
玉树临风的斑马  ·  C#WPF异常报错配置系统未能初始化缺少根元 ...·  2 周前    · 
老实的领结  ·  Element:mousemove 事件 ...·  6 月前    · 
礼貌的萝卜  ·  创建更新触发器_下一站的技术博客_51CTO博客·  2 年前    · 
酷酷的紫菜  ·  查询字段、运算符、宏和变量 - Azure ...·  2 年前    · 
痴情的油条  ·  python - Can I use ...·  2 年前    · 
Code  ›  nodejs解析xml文件开发者社区
var nodejs学习 xml语言
https://cloud.tencent.com/developer/article/1653903
傻傻的香烟
2 年前
作者头像
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]));
 
推荐文章
玉树临风的斑马  ·  C#WPF异常报错配置系统未能初始化缺少根元素_微软技术-CSDN问答
2 周前
老实的领结  ·  Element:mousemove 事件 - Web API | MDN
6 月前
礼貌的萝卜  ·  创建更新触发器_下一站的技术博客_51CTO博客
2 年前
酷酷的紫菜  ·  查询字段、运算符、宏和变量 - Azure Boards | Microsoft Learn
2 年前
痴情的油条  ·  python - Can I use Win32 COM to replace text inside a word document? - Stack Overflow
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号