Create individual xml files from one file with many xmls

Say you have a file with a bunch of nice-xml documents inside and you want them all as individual files. I come across this problem quite often so I thought it would be a good idea to have a script here showing how I usually sole this problem (PHP >= 4.3).

<?php
//Check the number of arguments
if($argc != 5){
    echo "Usage: splitxml <filename> <starttag> <endtag> <path>n";
    echo "<filename> - filename of file with xmls inn";
    echo "<starttag> - first tag of the xml you want to extractn";
    echo "<endtag> - last tag of the xml you want to extractn";
    echo "<path> - where you want the files to be created";
    exit();
}

// Read file into an array
$xml = file($argv[1]);

$starttag = $argv[2];
$endtag = $argv[3];
$resultpath = $argv[4];

$tmp = array();
$counter = 0;
$recording = false;

foreach($xml as $row){
    // Found start tag - start "recording"
    if(strpos($row, $starttag) !== false){
        $recording = true;
    }
    // Save all rows if "recording"
    if($recording){
        $tmp[] = $row;
    }
    // Found end tag - stop "recording" and save file
    if(strpos($row, $endtag) !== false){
        $fp = fopen($resultpath.'/file'.$counter++.'.xml', 'w');
        fwrite($fp, implode("",$tmp));
        fclose($fp);
        $tmp = array();
        $recording = false;
    }
}
?>

Here is an example of how the file can look (books.xml):

--------------------
<book>
    <title>Ronja</title>
    <author>Astrid Lindgren</author>
</book>
--------------------
<book>
    <title>Barnen i Bullerbyn</title>
    <author>Astrid Lindgren</author>
</book>
--------------------

To get these book xml into separate files just run the above script with the following parameters:

php splitxml books.xml '' '' books/

This will create the files file0.xml and file1.xml in the directory books/

file0.xml:

<book>
    <title>Ronja</title>
    <author>Astrid Lindgren</author>
</book>

file1.xml:

<book>
    <title>Barnen i Bullerbyn</title>
    <author>Astrid Lindgren</author>
</book>

Comments are closed.

Niklas Tech Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.