Page 1 of 1
Split an xml into small files
Posted: Fri Aug 05, 2011 7:49 pm
by bettinz
Hello,
sorry for my english, and sorry if the question is easy, but i've googled and i've find nothing

I need to write a script that read a big xml file, and split this file into small xml.
The structure is something like this:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<header>
<products>
<product>
<value1>
</value1>
<value2>
</value2>
</product>
<product>
<value1>
</value1>
<value2>
</value2>
</product>
</products>
</header>
One of the splitted files will be something like this:
file1.xml
Code: Select all
<header>
<products>
<product>
<value1>
</value1>
<value2>
</value2>
</product>
</products>
</header>
Then, one xml file for product (to test, in future i want to save 100 products on every splitter xml)
Do you think is possible?
Thanks to everyone

Re: Split an xml into small files
Posted: Fri Aug 05, 2011 9:04 pm
by Christopher
Do this:
Code: Select all
$obj = simplexml_load_file ('my_big_file.xml');
echo '<pre>' . print_r($obj, 1) . '</pre>';
Run it and look a the structure of $obj. You just need to iterate through the arrays and write out the small files.
Re: Split an xml into small files
Posted: Sat Aug 06, 2011 11:16 am
by bettinz
Thanks for the reply.
I've started to look into simplexml, and i've writes this:
Code: Select all
<?php
$xml= simplexml_load_file('articoli.xml');
$i=0;
foreach ($xml->UpdatedProducts->Product as $product) {
$id[$i]= $product->InternalID;
$code[$i] = $product->Code;
$descr[$i]= $product->Description;
$descrhtml[$i]= $product->DescriptionHtml;
$cat[$i]= $product->Category;
$subcat[$i]= $product->Subcategory;
$vat[$i]= $product->Vat;
$um[$i]= $product->Um;
$uno[$i]= $product->NetPrice1;
$unoa[$i]= $product->GrossPrice1;
$due[$i]= $product->NetPrice2;
$duea[$i]= $product->GrossPrice2;
$cinque[$i]= $product->NetPrice5;
$cinquea[$i]= $product->GrossPrice5;
$sette[$i]= $product->NetPrice7;
$settea[$i]= $product->GrossPrice7;
$otto[$i]= $product->NetPrice8;
$ottoa[$i]= $product->GrossPrice8;
$producer[$i]= $product->ProducerName;
$suppcode[$i]= $product->SupplierCode;
$suppname[$i]= $product->SupplierName;
$suppprodcode[$i]= $product->SupplierProductCode;
$suppnetprice[$i]= $product->SupplierNetPrice;
$suppgrossprice[$i]= $product->SupplierGrossPrice;
$sizeum[$i]= $product->SizeUm;
$weightum[$i]= $product->WeightUm;
$netweight[$i]= $product->NetWeight;
$grossweight[$i]= $product->GrossWeight;
$manage[$i]= $product->ManageWarehouse;
$qty[$i]= $product->AvailableQty;
$notes[$i]= $product->Notes;
$image[$i]= $product->ImageFileName;
$i++;
}
for ($x=0; $x<$i; $x++){
$myFile = "split[$x].xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = '<?xml version="1.0" encoding="UTF-8"?>';
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
$stringData = '<EasyfattProducts AppVersion="2" Creator="Danea Easyfatt Enterprise 2011.16c" CreatorUrl="http://www.danea.it/software/easyfatt" Mode="incremental" ImageFolder="\">';
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
$stringData = ' <UpdatedProducts>';
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
$stringData = '<Product>';
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
$stringData = "<InternalID>$id[$x]</InternalID>";
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
$stringData = '</Product>';
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
$stringData = ' </UpdatedProducts>';
fwrite($fh, $stringData);
$stringData = '<DeletedProducts></DeletedProducts>';
fwrite($fh, $stringData);
$stringData = '</EasyfattProducts>';
fwrite($fh, $stringData);
fclose($fh);
}
?>
Do you think i'm in the correct direction? It's the first time that I use simplexml, then this code is probably not optimized.
The real problem is that "<InternalID>$id[$x]</InternalID>": $id[0] it appear to be empty
Thanks for the help
note: the second version appear to work..but i want an opinion