node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

attrNameProcessors and tagNameProcessors options for builder

Open alexcrack opened this issue 10 years ago • 7 comments

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.

alexcrack avatar Mar 06 '15 13:03 alexcrack

Sure, sounds sensible to me.

Leonidas-from-XIV avatar Mar 12 '15 08:03 Leonidas-from-XIV

Done: https://github.com/Leonidas-from-XIV/node-xml2js/pull/462

jcsahnwaldt avatar Jun 15 '18 03:06 jcsahnwaldt

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?

jasonperrone avatar Jan 07 '23 19:01 jasonperrone

There is no support for these at the moment, this issue is a proposal for a feature.

Leonidas-from-XIV avatar Jan 07 '23 19:01 Leonidas-from-XIV

Ah. I misunderstood the "done" above.

jasonperrone avatar Jan 07 '23 20:01 jasonperrone

@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:.

jasonperrone avatar Jan 09 '23 12:01 jasonperrone

The only way I can figure to do it is:

  1. Start with my object.
  2. Use Builder to turn it into an XML string
  3. Use Parser to convert that String back into an object, and use the tagNameProcessors option on the parser.
  4. Use Builder again to turn that object into an XML string

Gotta be a better way.

jasonperrone avatar Jan 09 '23 12:01 jasonperrone