【发布时间】:2025-12-12 13:15:01
【问题描述】:
我有一段 XML 看起来像这样:
<errorMessage>
<payload encoding="plain">The error message</payload>
</errorMessage>
我使用 xml2js 解析器:
var parser = new xml2js.Parser({
explicitCharKey: false,
trim: true,
explicitRoot: true,
mergeAttrs: true
});
parser.parseString(myString, function(err, result) {
var payload = result.errorMessage.payload;
// how do I access the error message text?
var errorMessage = payload[0]['_'];
});
我需要访问payload 元素内的The error message 字符串。如果我在包含<payload> 的节点上使用_ 键,那么文本会被正确检索,但使用下划线魔术键看起来很可疑。
这是推荐的方式吗?有没有更清洁的方法?
【问题讨论】:
标签: javascript xml node.js xml-parsing