我有一个xml,其中标签名包含冒号(:),它看起来如下所示:
<samlp:Response>
data
</samlp:Response>我使用下面的代码将这个xml解析为json,但是不能使用它,因为标记名包含冒号。
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
var fs = require('fs');
fs.readFile(
filePath,
function(err,data){
if(!err){
parser.parseString(data, function (err, result) {
//Getting a linter warning/error at this point
console.log(result.samlp:Response);
});
}else{
callback('error while parsing assertion'+err);
}
}
);
};错误:
events.js:161
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'Response' of undefined如何在不更改XML内容的情况下成功解析此xml?

发布于 2017-04-09 21:36:36
xml2js允许您通过添加 array in your config options来明确设置XML名称空间删除。
const xml2js = require('xml2js')
const processors = xml2js.processors
const xmlParser = xml2js.Parser({
tagNameProcessors: [processors.stripPrefix]
})
const fs = require('fs')
fs.readFile(filepath, 'utf8', (err, data) => {
if (err) {
//handle error
console.log(err)
} else {
xmlParser.parseString(data, (err, result) => {
if (err) {
// handle error
console.log(err)
} else {
console.log(result)
}
})
}
})发布于 2020-05-04 22:06:21
我喜欢接受的答案,但请记住,您可以使用其密钥访问属性。
object['property']所以在你的情况下
result['samlp:Response']https://stackoverflow.com/questions/43312080
复制相似问题