英文:
How to extract data from xml in NodeJS?
问题
如何使用类似于NodeJS中的fast-xml-parser解析器提取numberMatched
的值?
英文:
I have the following xml:
<?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"/>
How can I extract the value of numberMatched
using an xml parser like fast-xml-parser in NodeJS?
答案1
得分: 0
你需要将 ignoreAttributes
选项设置为 false
。
import { XMLParser } from "fast-xml-parser";
const XMLdata = `<?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 parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: ""
});
let jObj = parser.parse(XMLdata);
console.log(jObj["wfs:FeatureCollection"].numberMatched);
英文:
You need to set ignoreAttributes
option to false
import { XMLParser } from "fast-xml-parser";
const XMLdata = `<?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 parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: ""
});
let jObj = parser.parse(XMLdata);
console.log(jObj["wfs:FeatureCollection"].numberMatched);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论