node-xml2js
node-xml2js copied to clipboard
attrNameProcessors and tagNameProcessors options for builder
For example we can add namespaces in builder and remove namespaces in parser (for the soap api). In result we have javascript objects without namespaces.
Sure, sounds sensible to me.
Done: https://github.com/Leonidas-from-XIV/node-xml2js/pull/462
I am on 0.4.23 and I can't get this to work:
const builder = new xml2js.Builder({tagNameProcessors: [upperMe]});
builder.buildObject(myObj);
function upperMe(name) {
return name.toUpperCase();
}
upperMe simply will not get called. What am I doing wrong?
There is no support for these at the moment, this issue is a proposal for a feature.
Ah. I misunderstood the "done" above.
@Leonidas-from-XIV without this feature, I can't seem to do what I need to. Let's say I have this object:
{
myRoot: {
$: {
'xmlns:abc': 'blahblah',
}
one: 'a',
two: 'b',
three: 'c'
}
}
How can I have the builder build the tags with the abc: prefix so that I don't have to change the names of the attributes in my object? In other words, I want the XML to look like:
<abc:myRoot xmlns:abc='blahblah'>
<abc:one>a</abc:one>
<abc:two>b</abc:two>
<abc:three>c</abc:three>
</abc:myRoot>
I honestly don't understand why it doesn't just do that since I did provide a namespace in the root tag.
So now I have to manually specify attributes in my object like 'abc:one' instead of one:.
The only way I can figure to do it is:
- Start with my object.
- Use Builder to turn it into an XML string
- Use Parser to convert that String back into an object, and use the tagNameProcessors option on the parser.
- Use Builder again to turn that object into an XML string
Gotta be a better way.