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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.