0

I'm having trouble creating a XML document with a CDATA section. Given this code:

const xmlObj = {
    "s:Envelope": {
        '@xmlns:s': http://schemas.xmlsoap.org/soap/envelope/",
         "s:Body": {
            "DoStuff": {
                '@xmlns': "https://randomUrl",
                'XmlRequest': {
                    '$': {
                         'test': 'apples'
                     }                                                                
                 }
             }
          }
        }
    }
    
    const final = xmlBuilder.create(xmlObj).end({ prettyPrint: true});    

    return final;    

What I get is this:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <DoStuff xmlns="https://randomUrl">
      <XmlRequest><![CDATA[[object Object]]]></XmlRequest>
    </DoStuff>
  </s:Body>
</s:Envelope>

I'd like it to be like this:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <DoStuff xmlns="https://randomUrl">
      <XmlRequest>
         <![CDATA[<test>
           apples
         </test>]]>
      </XmlRequest>
    </DoStuff>
  </s:Body>
</s:Envelope>

The issue is the [Object object] part

Nigel
  • 985
  • 1
  • 11
  • 16

1 Answers1

0

By definition, a CDATA section does not contain nodes: CDATA means "character data". The character data might be something that looks like XML (for example <![CDATA[<test>apples</test>]]>) but that's an optical illusion: the purpose of the CDATA tag is to say "even if the stuff in here looks like markup, it isn't".

So the way to do this is to create your nodes, serialize them to a string, and then insert this string into the final document.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164