Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding namspace in xml using a xmlbuilder in nodejs

I am generating xml in nodejs by using xmlbulilder package, now my requirement is to add namespace to xml. for example

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

how we can do it? Thanks for help!

like image 514
Sachin Avatar asked Sep 28 '15 13:09

Sachin


1 Answers

I found that you can accomplish it through code like below.

(() => {
    '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);
})();

I don't know if there is a more explicit way of setting namespace, but it would make sense to have one to reduce redundancy.

like image 119
Dmitry Avatar answered Oct 13 '22 22:10

Dmitry