Skip to content

Instantly share code, notes, and snippets.

@piotr-galas
Created September 1, 2015 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotr-galas/d26190e201b5a5161aa2 to your computer and use it in GitHub Desktop.
Save piotr-galas/d26190e201b5a5161aa2 to your computer and use it in GitHub Desktop.
split xml to smaller one
<?php
class Lmnts_AdsImporter_Model_XmlSpliter
{
const ITEM_NUMBER = 6;
private $xmls = array();
public function splitAllFiles()
{
$pathManager = Mage::getModel('adsimporter/pathManager');
$paths = $pathManager->getXmlPaths();
foreach($paths as $path){
$this->splitIntoSmaller($path);
}
}
private function splitIntoSmaller($xmlPath)
{
$baseXml = new DOMDocument();
$baseXml->load($xmlPath);
$countItem = $baseXml->getElementsByTagName("item")->length;
$nodes = $this->getAMDNodes($baseXml); //get array witch add, modify , and delete node
$this->split($nodes, $countItem);
$this->createFiles();
exit();
}
private function createFiles()
{
}
private function split($nodes, $countItem)
{
$iteration = 0;
$smallerXml = null;
foreach ($nodes as $key =>$node){
foreach ($node->getElementsByTagName("item") as $item){
$smallerXml = $this->createOrGetXml( $iteration, $smallerXml);
$node = $smallerXml->getElementsByTagName($key)->item(0); // node = add || modify || delete
$item = $smallerXml->importNode($item, true);
$node->appendChild($item);
if( $iteration == $countItem - 1){
$this->xmls[] = $smallerXml->saveXML();
}
$iteration++;
}
}
}
private function getAMDNodes($baseXml)
{
return array(
'add' => $baseXml->getElementsByTagName("add")->item(0),
'modify' => $baseXml->getElementsByTagName("modify")->item(0),
'delete' => $baseXml->getElementsByTagName("delete")->item(0),
);
}
private function shouldBeNewDocument($iteration)
{
if($iteration % self::ITEM_NUMBER == 0){
return true;
}
}
private function createOrGetXml($iteration, $smallerXml)
{
if($this->shouldBeNewDocument($iteration)){
if($iteration != 0){
$this->xmls[] = $smallerXml->saveXML();
}
return $this->createXmlSkelet();
}
return $smallerXml;
}
private function createXmlSkelet()
{
$newdoc = new DOMDocument();
$newdoc->formatOutput = true;
$content = $newdoc->createElement('content');
$add = $newdoc->createElement('add');
$modify = $newdoc->createElement('modify');
$delete = $newdoc->createElement('delete');
$content = $newdoc->appendChild($content);
$content->appendChild($add);
$content->appendChild($modify);
$content->appendChild($delete);
return $newdoc;
}
public function getXmlStructure(SimpleXMLElement $xml)
{
$xml = new SimpleXMLElement('<content></content>');
$xml->addChild('add');
$xml->addChild('modify');
$xml->addChild('delete');
return $xml;
}
private function createFile($fileName, $content)
{
$pathManager = Mage::getModel('adsimporter/pathManager');
$io = new Varien_Io_File();
$io->open(array('path'=>$pathManager->getDirectoryPath() ));
$io->filePutContent($fileName, $content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment