Posted by Pavel Lepin on 08/10/07 09:53
Bob Bedford <bob@bedford.com> wrote in
<46bc22e5$0$3808$5402220f@news.sunrise.ch>:
> What I'd like to do is split the large XML file (can be
> more than 30MB) in little parts and keep the header for
> every file.
>
> <total>
> <head>
> </head>
> <info>
> </info>
> <info>
> </info>
> <info>
> </info>
> ...
> </total>
>
> The only change is the amount of "info" available. What
> I'd like is to split the file to create littles ones whit
> the same <head></head> datas but each with less <info>
> tags (say limited to 3 for every file).
<?php
error_reporting (E_ALL | E_STRICT) ;
define ('MAX_ITEMS' , 3) ; $infoList = array () ;
$doc = new DOMDocument () ; $doc->load ('split.xml') ;
foreach
(
$infos = $doc->getElementsByTagName ('info') as $info
)
$infoList [] = $info->cloneNode (TRUE) ;
for ($i = $infos->length - 1 ; $i >= 0 ; -- $i)
$infos->item ($i)->parentNode->removeChild
($infos->item ($i)) ;
for ($i = 1 ; count ($infoList) ; ++ $i)
{
$curDoc = new DOMDocument () ;
$curDoc->appendChild
(
$curDoc->importNode ($doc->documentElement , TRUE)
) ;
for ($j = MAX_ITEMS ; $j && $info ; -- $j)
if (! ($info = array_shift ($infoList))) break ;
else
$curDoc->documentElement->appendChild
($curDoc->importNode ($info , TRUE)) ;
$curDoc->save ('split_' . $i . '.xml') ;
}
?>
--
"Patience is a minor form of despair, disguised as
virtue." -- Ambrose Bierce
[Back to original message]
|