PHP - Array to XML with CDATA

Junior Maia

I need to create an xml to send via soap, but I can not. I need an xml with a CDATA tag and I can not do it. The xml must be in this format:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="url-here">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:callServiceMethod>
         <!--Optional:-->
         <xml>
         <![CDATA[<fruits>
                    <type>G</type>
                    <flav>grape</flav>
                    <orders>
                        <order>
                            <client>12345</client>
                            <itens>
                                <item>
                                    <cod>1</cod>
                                    <name>Grape</name>
                                </item>
                                <item>
                                    <cod>2</cod>
                                    <name>Apple</name>
                                </item>
                            </itens>
                        </order>
                    </orders>
                </fruits>]]>
        </xml>
      </ser:callServiceMethod>
   </soapenv:Body>
</soapenv:Envelope>

I tried to create an array to generate the xml, this works, but without the CDATA, like this:

$soapArgs = array(
    'xml' => array(
    //need CDATA here
        'fruits' => array(
            'type' => 'G',
            'flav' => 'grape',
            'orders' => array(
                'order' => array(
                    'client' => 12345,
                    'itens' => array(
                        'item' => array(
                            'cod' => 1,
                            'name' => 'Grape',
                        ),
                        'item' => array(
                            'cod' => 2,
                            'name' => 'Apple',
                        ),
                    )
                )
            )
        )
    )
);

$soapClient = new SoapClient($params);
$serviceResponse = $soapClient->callServiceMethod($soapArgs);

How do I generate this xml with CDATA to be able to send via SOAP?

Thank you guys!

IMSoP

As discussed under What does <![CDATA[]]> in XML mean? a CDATA section is a way of including a string in an XML document without it being parsed. Everything from <fruits> to </fruits> is therefore not part of the actual XML document; as far as the SOAP request is concerned, it's just a string.

(This kind of thing is quite common in SOAP services, because people find it easier to parse the payload outside of their SOAP code, so they use SOAP as a large wrapper around arbitrary content. It could just as well be <json><![CDATA[{"foo": "bar"}]]></json> or some other format of payload.)

Assuming the owner of the SOAP service hasn't done something really weird, this:

   <xml>
     <![CDATA[<fruits>
                <type>G</type>
                <flav>grape</flav>
                <orders>
                    <order>
                        <client>12345</client>
                        <itens>
                            <item>
                                <cod>1</cod>
                                <name>Grape</name>
                            </item>
                            <item>
                                <cod>2</cod>
                                <name>Apple</name>
                            </item>
                        </itens>
                    </order>
                </orders>
            </fruits>]]>
   </xml>

should be completely equivalent to this:

   <xml>
           &lt;fruits&gt;
                &lt;type&gt;G&lt;/type&gt;
                &lt;flav&gt;grape&lt;/flav&gt;
                &lt;orders&gt;
                    &lt;order&gt;
                        &lt;client&gt;12345&lt;/client&gt;
                        &lt;itens&gt;
                            &lt;item&gt;
                                &lt;cod&gt;1&lt;/cod&gt;
                                &lt;name&gt;Grape&lt;/name&gt;
                            &lt;/item&gt;
                            &lt;item&gt;
                                &lt;cod&gt;2&lt;/cod&gt;
                                &lt;name&gt;Apple&lt;/name&gt;
                            &lt;/item&gt;
                        &lt;/itens&gt;
                    &lt;/order&gt;
                &lt;/orders&gt;
            &lt;/fruits&gt;
   </xml>

The function creating the XML will handle escaping the string, or wrapping it in CDATA, so all you should need is:

$xml = '<fruits>
                <type>G</type>
                <flav>grape</flav>
                <orders>
                    <order>
                        <client>12345</client>
                        <itens>
                            <item>
                                <cod>1</cod>
                                <name>Grape</name>
                            </item>
                            <item>
                                <cod>2</cod>
                                <name>Apple</name>
                            </item>
                        </itens>
                    </order>
                </orders>
            </fruits>';
$soapArgs = array('xml' => $xml);

How you create the string $xml is, obviously, up to you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related