Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP to XML insert array
    text
    copied!<p>I have an XML string that I have built up using the following example:</p> <pre><code>//add root $Add = $dom-&gt;appendChild($dom-&gt;createElement('Add')); //pCase $pCase = $Add-&gt;appendChild($dom-&gt;createElement('pCase')); //add elements $LeadGenID = $pCase-&gt;appendChild($dom-&gt;createElement('LeadGenID')); $LeadGenID-&gt;appendChild($dom-&gt;createTextNode('22')); $Debtor = $pCase-&gt;appendChild($dom-&gt;createElement('Debtor')); //address fields $Addresses = $Debtor-&gt;appendChild($dom-&gt;createElement('Addresses')); $Address = $Addresses-&gt;appendChild($dom-&gt;createElement('Address')); //array of address objects should go here $test1 = $dom-&gt;saveXML(); $sxe = simplexml_load_string($test1); if ($sxe === false) { echo 'Error while parsing the document'; exit; } $dom_sxe = dom_import_simplexml($sxe); if (!$dom_sxe) { echo 'Error while converting XML'; exit; } $dom = new DOMDocument('1.0', 'utf-8'); $dom_sxe = $dom-&gt;importNode($dom_sxe, true); $dom_sxe = $dom-&gt;appendChild($dom_sxe); echo $dom-&gt;save('application.xml'); </code></pre> <p>This then outputs the following in my XML:</p> <pre><code>&lt;Add&gt; &lt;pCase&gt; &lt;LeadGenID&gt;22&lt;/LeadGenID&gt; &lt;Debtor&gt; &lt;Addresses&gt;&lt;Address/&gt;&lt;/Addresses&gt; &lt;/Debtor&gt; &lt;/pCase&gt; </code></pre> <p></p> <p>I also need to output an array into this XML, so that the full output is as follows:</p> <pre><code>&lt;Add&gt; &lt;pCase&gt; &lt;LeadGenID&gt;22&lt;/LeadGenID&gt; &lt;Debtor&gt; &lt;Addresses&gt; &lt;Address&gt; &lt;Street&gt;Some street&lt;/Street&gt; &lt;Postcode&gt;Some postcode&lt;/Postcode&gt; &lt;/Address&gt; &lt;/Addresses&gt; &lt;/Debtor&gt; &lt;/pCase&gt; </code></pre> <p></p> <p>I have tried to accomplish this using</p> <pre><code>$address_array = array ( $_POST['Street'] =&gt; 'Street', $_POST['Postcode'] =&gt; 'Postcode' ); $xml = new SimpleXMLElement('&lt;Address/&gt;'); array_walk_recursive($address_array, array ($xml, 'addChild')); </code></pre> <p>but at this point I am completely lost. The API I have been given is sparse on documentation to say the least but I'm getting closer with it, but I need to enter the address as an array for the gateway. I am new to PHP as I am primarily a frontend developer who has usually only done simple contact forms etc in PHP.</p> <p>How can I output the XML example I've given above?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload