问题描述
我正在尝试使用 xml2js 解析 nestjs 中的 XML 文件,到目前为止一切正常,但现在我遇到了一个问题:
要解析的 XML:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="example_text">
Hi there MR. <xliff:g id="mister" example="John">%1$s</xliff:g> and Mrs. <xliff:g id="miss" example="Jessica">%1$s</xliff:g>
</string>
</resources>
xml2js 在代码中的使用:
const xmlParser = require('xml2js').Parser({trim: true,ignoreAttrs: false,mergeAttrs: true});
...
const xml = entry.getData().toString('utf8');
xmlParser.parseString(xml,function(err,result) {
console.log('Result: ',result);
if (result && result.resources.string) {
const strings = result.resources.string;
strings.map(x => {
console.log('String: ',x);
});
}
});
输出:
Result: { resources:
{ 'xmlns:xliff': [ 'urn:oasis:names:tc:xliff:document:1.2' ],string: [ [Object] ] } }
String: { _: 'Hi there MR. and Mrs.',name: [ 'example_text' ],'xliff:g':
[ { _: '%1$s',id: [Array],example: [Array] },{ _: '%1$s',example: [Array] } ] }
现在我的问题:
为什么我需要这些信息:
- 我想用其他东西替换 %1$s 并存储 xliff 中包含的信息
有没有办法用 xml2js 来做到这一点,或者我是否需要另一个库来解析 xml,以获得所需的信息?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)