Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

Add prefix to XML #570

Open JoseAnt86 opened 4 years ago

JoseAnt86 commented 4 years ago

When going from XML to JS, the "processors.stripPrefix" allows you to remove the prefix. Is it possible for the Builder to have an option for addPrefix?

const jsonObj = { foo: { bar: { hello: 'world' } } }; const builder = new xml2js.Builder({addPrefix: 'n1'}); const xml = builder.buildObject(jsonObj); console.log(xml);

world
dhruvsaxena1998 commented 2 years ago

I don't think there isn't any yet but if you want you can achieve this using tagNameProcessors

const parser = new xml2js.Parser({
  tagNameProcessors: [(tag) => `${prefix}:${tag}`],
});

await parser.parseStringPromise(xmlString);