<?php
/**
* Part of SimpleViewer.net portfolio web site package.
*
* @package Showkase
* @author Jack Hardie {@link http://www.jhardie.com}
* @copyright Copyright (c) 2012, SimpleViewer Inc.
*/
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
/**
* Reads and writes xml file
*
* @package Showkase
*/
class Xml
{
/**
* Read xml file and parse into structure
*
* @access private
* @returns array of $vals and $index created by xml_parse_into_struct. False if no xml file.
* @param string path to xml file
*/
/**
* @var string path to xml file
*/
protected $xmlPath;
/**
* @var object DOM document
*/
public $domDoc;
/**
* Constructs Xml
*
* @param string path to xml file
*/
public function __construct($xmlPath)
{
$this->xmlPath = $xmlPath;
libxml_use_internal_errors(true);
$this->loadXml();
}
/**
* Load xml from file and create dom object
*
* @access public
* @return boolean success
*/
public function loadXml()
{
$message = '';
$xmlSource = @file_get_contents($this->xmlPath);
if ($xmlSource === false)
{
throw new Exception('loadXml cannot read <span class="filename">'.basename($this->xmlPath).'</span>');
}
if ($xmlSource == '')
{
throw new Exception('xml file '.$this->xmlPath.' is empty');
}
$this->domDoc = new DOMDocument();
$this->domDoc->preserveWhiteSpace = false;
$loaded = $this->domDoc->loadXML($xmlSource);
if ($loaded === false)
{
$errors = libxml_get_errors();
foreach ($errors as $error)
{
$message .= 'XML error in '.basename($this->xmlPath).', line '.$error->line.'. '.$error->message.'<br />';
}
libxml_clear_errors();
throw new Exception($message);
}
return true;
}
/**
* Return domDoc
*
* @return object
*/
public function getDomDoc()
{
return $this->domDoc;
}
}