Skip to content

Instantly share code, notes, and snippets.

@PetraMotz
Last active May 28, 2023 21:12
Show Gist options
  • Save PetraMotz/58dc5120bc7bf4173174aee8a23a8ce4 to your computer and use it in GitHub Desktop.
Save PetraMotz/58dc5120bc7bf4173174aee8a23a8ce4 to your computer and use it in GitHub Desktop.
PHP #xml #php
/**
* writes and saves xml file with given data
* @param array $data
* @param array $order
*/
private function writeXmlFile($data, $orderArray){
$today = date_format( new \DateTime(), 'Y-m-d\TH:i:s'); //2018-07-25T11:20:12
$orderNr = 'gbp'.date_format(new \DateTime(), 'y\TH:i:s');
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><orders/>');
$order = $xml->addChild('order');
$order->addChild('number', $orderNr);
$order->addChild('createdAt', $today);
$order->addChild('addressShippingCompany');
$order->addChild('addressShippingFirstname', $data['firstname']);
$order->addChild('addressShippingLastname', $data['lastname']);
$order->addChild('addressShippingStreet', $data['address']);
$order->addChild('addressShippingZipcode', $data['zip']);
$order->addChild('addressShippingCity', $data['city']);
$order->addChild('addressShippingCountry', $data['country']);
$order->addChild('addressShippingEmail', $data['mail']);
$order->addChild('addressShippingPhone', '000');
$order_lines = $order->addChild('orderLines');
foreach ($orderArray as $key => $orderLine){
if($orderLine != ''){
$brochure = $this->brochureRepository->findByUid($key);
$sku = $brochure->getSkuNr();
$order_line = $order_lines->addChild('orderLine');
$order_line->addChild('sku', $sku);
DebuggerUtility::var_dump($orderLine);
$order_line->addChild('productTitle', htmlspecialchars($orderLine));
$order_line->addChild('quantityOrdered', '1');
}
}
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = true;
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/fileadmin/userdaten/Bestellungen_XML/Bestellung'.$orderNr.'.xml', $dom->saveXML());
}
Wie in HTML müssen auch in XML Sonderzeichen speziell formatiert werden. Die fünf Zeichen &, ', <, > und " werden wie in HTML angegeben:
& &amp;
' &apos;
< &lt;
> &gt;
" &quot;
Umlaute und das ß müssen aber so definiert werden:
Ä &#196;
Ö &#214;
Ü &#220;
ä &#228
ö &#246;
ü &#252;
ß &#223;
Beispiele:
Das Zeichen &amp; (&quot;ampersand&quot;) wird durch ein Entity ersetzt.
M&#252;ller, M&#220;LLER
Ausgabe:
Das Zeichen & ("ampersand") wird durch ein Entity ersetzt.
Müller, MÜLLER
htmlspecialchars($orderLine)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment