5

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!

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.