Skip to content

Instantly share code, notes, and snippets.

@rifki
Last active April 2, 2016 03:22
Show Gist options
  • Save rifki/65fb451d501eed8996742ff2af2578ae to your computer and use it in GitHub Desktop.
Save rifki/65fb451d501eed8996742ff2af2578ae to your computer and use it in GitHub Desktop.
Append XML File
<?php
// format xml
/*
<products>
<product>
....
....
<product>
</products>
*/
$xmlFile = '/path/to/xml/file.xml'; // url or path
$doc = new \DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$doc->load($xmlFile);
$fragment = $doc->createDocumentFragment();
$elementChild = <<<elementChild
<product>
<name>WINSTON SMITH</name>
<producturl>http://www.example.com/product/wanita/sepatu/natalia-lace-up-flats</producturl>
<price>50000</price>
<discount>0</discount>
<description>
Winston Smith Lady posses the concept of beauty, simplicity, style and comfort to produce premium quality shoes. Their are aiming to share their vision for comfortable, smart-casualshoes at pocket friendly prices. Natalia is a lace flat shoes made of 100% high quality leather. It's a versatile pair that you can wear with any outfit.
</description>
<instock>8</instock>
<categotyid1>shop</categotyid1>
<categotyid2>directory</categotyid2>
<categotyid3>brands</categotyid3>
</product>
elementChild;
$fragment->appendXML($elementChild);
$doc->documentElement->appendChild($fragment);
//echo $doc->saveXML(); // dump
$doc->save($xmlFile);// Save as xml file
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment