You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在NodeJS中使用fast-xml-parser提取XML的numberMatched值

使用fast-xml-parser在Node.js中提取XML的numberMatched值

步骤1:安装依赖

先在Node.js项目中安装fast-xml-parser:

npm install fast-xml-parser

步骤2:编写解析代码

通过配置解析选项处理XML的命名空间,再提取目标属性值:

const { XMLParser } = require('fast-xml-parser');

// 待解析的XML内容
const xmlContent = `<?xml version="1.0" encoding="utf-8"?>
    <wfs:FeatureCollection xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.opengis.net/wfs/2.0 http://www.wfs.nrw.de/aaa-suite/schema/ogc/wfs/2.0/wfs.xsd" timeStamp="2023-06-01T13:31:53.444+02:00" numberReturned="0" numberMatched="9359426"/>`;

// 解析配置:忽略命名空间,属性直接用原名称访问
const parserConfig = {
    ignoreNameSpace: true,
    attributeNamePrefix: ""
};

// 初始化解析器并解析XML
const parser = new XMLParser(parserConfig);
const parsedData = parser.parse(xmlContent);

// 提取numberMatched属性值
const numberMatched = parsedData.FeatureCollection.numberMatched;
console.log(numberMatched); // 输出结果:9359426

补充说明

如果不想忽略命名空间,也可以直接通过带命名空间的节点名访问:

// 不设置ignoreNameSpace时的访问方式
const numberMatched = parsedData['wfs:FeatureCollection'].numberMatched;

内容的提问来源于stack exchange,提问作者GongFu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:52:42