1
我嘗試使用nodeJS來訪問現在代碼中存儲爲字符串的XML中的所有數據。實際上,當我想要深入時,我只能成功訪問主節點,但不是所有的孩子。如何從nodeJS中的XML解析器中獲取數據
這是我的代碼,我嘗試獲取ms中的所有數據:IDENT節點。
var http = require('http');
var xml2js = require('xml2js');
var extractedData = "";
var parser = new xml2js.Parser();
var xml = "<wfs:FeatureCollection><gml:boundedBy><gml:Box srsName='EPSG:3945'><gml:coordinates>1399644.376399,4179041.966594 1426575.406024,4209057.424111</gml:coordinates></gml:Box></gml:boundedBy><gml:featureMember><ms:SV_ARRET_P fid='SV_ARRET_P.1484'><gml:boundedBy><gml:Box srsName='EPSG:3945'><gml:coordinates>1418152.331881,4208150.797551 1418152.331881,4208150.797551</gml:coordinates></gml:Box></gml:boundedBy><ms:msGeometry><gml:Point srsName='EPSG:3945'><gml:coordinates>1418152.331881,4208150.797551</gml:coordinates></gml:Point></ms:msGeometry><ms:GID>1484</ms:GID><ms:GEOM_O>196</ms:GEOM_O><ms:IDENT>FLA92A</ms:IDENT><ms:GROUPE>FLA92</ms:GROUPE><ms:LIBELLE>Fort Lajard</ms:LIBELLE><ms:TYPE>BUS</ms:TYPE><ms:CDATE>2017-01-06T14:15:10</ms:CDATE><ms:MDATE>2017-01-06T14:15:10</ms:MDATE></ms:SV_ARRET_P></gml:featureMember></wfs:FeatureCollection>"
parser.parseString(xml, function(err,result){
//Extract the value from the data element
xml = result['wfs:FeatureCollection'];
parser.parseString(xml, function(err,result){
extractedData = result['wfs:FeatureCollection']['gml:featureMember']['ms:SV_ARRET_P']['ms:IDENT'];
console.log(extractedData);
});
});
感謝您的幫助提前
你是說'extractData'不是你期望的值嗎?你看過「結果」的價值嗎?它的結構是什麼?可能是[Access/process(嵌套)對象,數組或JSON]的副本(http://stackoverflow.com/q/11922383/218196)。 –
爲什麼要在parser.parseString(xml,....){...}'裏再次解析XML * *?你已經解析了xml,結果應該是一個完整的樹,而不是你現在可以訪問的,只需諮詢一下。 –