0

I am going to send XML response from my node js api. Currently I am using xml - npm package

When I am sending data like following

res.set('Content-Type', 'text/xml');

let example5 = [
  {
    toys: [
      {
        _attr: {
          decade: '80s',
          locale: 'US'
        }
      },
      {
        toy: 'Transformers'
      },
      {
        toy: [
          {
            _attr: {
              knowing: 'half the battle'
            }
          },
          'GI Joe'
        ]
      },
      {
        toy: [
          {
            name: 'He-man'
          },
          {
            'g:brand': 'He-man'
          },
          {
            description: {
              _cdata: '<strong>Master of the Universe!</strong>'
            }
          }
        ]
      }
    ]
  }
]

return res.send(xml(example5, true));

When using the key with : it is giving error like this

enter image description here

Shailesh Daund
  • 170
  • 1
  • 11

1 Answers1

0

Colon in the tag means namespace prefix, and because you're missing a g: namespace, it's rather not a valid XML, than a node-xml problem.

you need to define namespace, looks like Google Merchant, so try adding it in the root's _attr like this:

 {
    toys: [
      {
        _attr: {
          decade: '80s',
          locale: 'US',   
          "xmlns:g": "http://base.google.com/ns/1.0"
          //...

check: What does the XML syntax with a colon mean?

traynor
  • 5,490
  • 3
  • 13
  • 23