gpt4 book ai didi

xml - 使用 nodejs 中的 xmlbuilder 在 xml 中添加 namspace

转载 作者:数据小太阳 更新时间:2023-10-29 02:04:48 26 4
gpt4 key购买 nike

我正在使用 xmlbulilder 包在 nodejs 中生成 xml,现在我的要求是将命名空间添加到 xml。例如

<nsA:root xmlns:nsA="namespaceA" xmlns:nsB="namespaceB">
<nsB:nodeA attrC="valC">nodeText</nsB:nodeA>
</nsA:root>

我们该怎么做?感谢您的帮助!

最佳答案

我发现你可以通过下面的代码来完成它。

(() => {
'use strict';

const xmlbuilder = require('xmlbuilder');

const doc = xmlbuilder.create('nsA:root')
.att('xmlns:nsA', 'namespaceA')
.att('xmlns:nsB', 'namespaceB')
.ele('nsB:nodeA', 'nodeText')
.att('attrC', 'valC');

const output = doc.end({pretty: true});

console.log(output);
})();

我不知道是否有更明确的命名空间设置方法,但有一个方法可以减少冗余。

关于xml - 使用 nodejs 中的 xmlbuilder 在 xml 中添加 namspace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32824517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com