Last active
May 28, 2023 21:12
-
-
Save PetraMotz/58dc5120bc7bf4173174aee8a23a8ce4 to your computer and use it in GitHub Desktop.
PHP #xml #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Wie in HTML müssen auch in XML Sonderzeichen speziell formatiert werden. Die fünf Zeichen &, ', <, > und " werden wie in HTML angegeben: | |
& & | |
' ' | |
< < | |
> > | |
" " | |
Umlaute und das ß müssen aber so definiert werden: | |
Ä Ä | |
Ö Ö | |
Ü Ü | |
ä ä | |
ö ö | |
ü ü | |
ß ß | |
Beispiele: | |
Das Zeichen & ("ampersand") wird durch ein Entity ersetzt. | |
Müller, MÜ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