How to automatically prepend namespace to tags when bulding XML from object?

61 Views Asked by At

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. I tried passing a tagNameProcessor function to the Builder() constructor and that didn't work.

So now I have to manually specify attributes in my object like 'abc:one' instead of one:.

1

There are 1 best solutions below

0
On

The answer is simply that you do use tagNameProcessor, but not with the Builder(). That functionality does not exist as of this writing. You do it in Parser(). That means if you use build to turn an Object into an XML string, once you have that string, additionally have Parser process the string.

Honestly, I just end up switching to xmlbuilder. Since this is not about xmlbuilder I won't post the answer here but let me know if you need an example.