Location: PHPKode > scripts > Impleo Music Collection Script > impleo/classes/XMLImportExport.class.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

require_once(dirname(__FILE__) . '/Factory.class.php');

/**
 * Class responsible for importing music items from an XML file and exporting to an XML file.
 *
 */
class XMLImportExport
{
    private $factory;

    public function __construct()
	{
        $this->factory = Factory::getInstance();
	}

    /**
     * Imports music collection from an XML file to the database.
     * Existing music items will be overwritten and new items will be added.
     *
     * @param string $xmlString Contents of the XML file to import from.
     * @param bool $popnu True if XML is from pop.nu. False by default.
     * @return array|false Returns an array containing number of added and updated items, or false if XML couldnt be parsed.
     */
    public function importXML($xmlString, $popnu)
    {
        $xml = @simplexml_load_string($xmlString);
        $detailInfo = $this->factory->getDetailInfo();

        if ($xml)
        {
            // Get records
            if (count($xml->record) > 0)
            {
                $countAdded = 0;
                $countUpdated = 0;

                foreach($xml->record as $objItem)
                {
                    $attributes = $objItem->attributes();

                    if (!$popnu)
                    {
                        if (empty($attributes['id']))
                            return false; // No ID
                    }
                    
                    if (strlen($objItem->artist) == 0 || strlen($objItem->title) == 0 || strlen($objItem->year_release) == 0) { return false; } // Missing artist, title or year

                    // Create detail array:
                    $arrDetails = array();
                    foreach ($detailInfo as $arrDetail)
                    {
                        if (!empty($objItem->$arrDetail['key']))
                            $arrDetails[$arrDetail['name']] = (string)$objItem->$arrDetail['key'];
                    }

                    // Update or insert
                    if ($this->factory->itemExists($attributes['id']))
                    {
                        $this->factory->updateRecord($attributes['id'], $objItem->artist, $objItem->title, $objItem->year_release, $arrDetails, null);
                        $countUpdated++;
                    }
                    else
                    {
                        $this->factory->insertRecord($objItem->artist, $objItem->title, $objItem->year_release, $arrDetails, null);
                        $countAdded++;
                    }

                }

                // Done
                return array("added" => $countAdded, "updated" => $countUpdated);
            }
            else
                return false; // Not right XML format (no records)
        }
        else
            return false; // Couldnt parse XML string
    }

    /**
     * Exports the entire collection to XML
     *
     * @return string Collection in XML format
     */
    public function exportXML()
    {
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
        $xml.= "<collection>\n";
        $arrMusicItems = $this->factory->getMusicItems();
        foreach($arrMusicItems as $item)
        {
            $xml.= "  <record id=\"" . $item->getID() . "\" added=\"" . $item->getDateAdded() . "\">\n";
            $xml.= "    <artist>" . $item->getArtist() . "</artist>\n";
            $xml.= "    <title>" . $item->getTitle() . "</title>\n";
            $xml.= "    <year_release>" . $item->getYear() . "</year_release>\n";
            foreach($item->getDetailInfo() as $key => $detail)
            {
                $xml.= "    <" . $key . ">" . $detail . "</" . $key . ">\n";
            }
            $xml.= "  </record>\n";
        }
        $xml.= "</collection>";
        return $xml;
    }
}
?>
Return current item: Impleo Music Collection Script