V průběhu dne jsem dokončil tu třídu. Není to úplně dokonalý ale účel to splní.
<?php
class ImportException extends Exception
{
}
class Import
{
/** @var fopen resource */
protected $handle;
/** @var XML parser object */
protected $parser;
/** @var bool */
protected $opened = false;
/** @var bool */
protected $feof = false;
/** @var string */
protected $item;
/** @var string */
protected $element;
/** @var array */
protected $parsed = array();
/** @var integer */
protected $length = 4096;
/**
* konstructor
*/
public function __construct()
{
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "onStartElement", "onEndElement");
xml_set_character_data_handler($this->parser, "onContent");
}
/**
* destructor
*/
public function __destruct()
{
xml_parser_free($this->parser);
}
/**
*
* @param strign $name
*/
public function setElement($name)
{
$this->element = $name;
}
/**
*
*/
public function parse()
{
if (empty($this->element))
throw new ImportException('Jmeno elementu nesmi byt prazdne!');
if ($this->feof)
return false;
# do-while nacita tak dlouho, dokud v poli $this->parsed neni aspon jeden element
do {
$data = fread($this->handle, $this->length);
$this->feof = feof($this->handle);
xml_parse($this->parser, $data, $this->feof);
# Pokud pole $this->parsed obsahuje aspon jeden element,
# bude pole vyprazdneno a metoda vrati jeho obsah
if (!empty($this->parsed)) {
$parsed = $this->parsed;
$this->parsed = array();
return $parsed;
}
} while (true);
}
/**
* Otevre soubor pro cteni
*
* @param string $url
*/
public function open($url)
{
# Zkontroluje nastaveni php.ini
if (ini_get('allow_url_fopen') === false) {
# Pokud neni mozne nastavit php.ini, vyhodi vyjimku
if (!is_callable('ini_set'))
throw new ImportException('Neni mozne otevrit soubor z url! Funkce ini_set() neni povolena.');
# Nastavi php.ini
ini_set('allow_url_fopen', true);
}
# Otevre soubor z url
if (!$this->handle = fopen($url, "rb")) {
throw new ImportException("Nepodarilo se otevrit soubor z url '" . $url . "'!");
}
}
/**
*
* @param resource $parser
* @param string $name
* @param array $attributes
*/
protected function onStartElement($parser, $name, array $attributes)
{
if ($name === $this->element) {
$this->opened = true;
$this->item = '<' . $name . '>';
#$this->byte = xml_get_current_byte_index($parser);
} elseif ($this->opened === true) {
$this->item .= '<' . $name . '>';
}
}
/**
*
* @param resource $parser
* @param string $name
*/
protected function onEndElement($parser, $name)
{
if ($name === $this->element) {
$this->opened = false;
$this->item .= '</' . $name . '>';
#$this->offset = xml_get_current_byte_index($parser);
$this->parsed[] = simplexml_load_string($this->item);
}
elseif ($this->opened === true) {
$this->item .= '</' . $name . '>';
}
}
/**
*
* @param resource $parser
* @param string $data
*/
protected function onContent($parser, $data)
{
if ($this->opened === true) {
$this->item .= htmlspecialchars($data);
#$this->byte = xml_get_current_byte_index($parser);
}
}
}
Příklad použití:
$url = 'example.com/soubor.xml';
$imp = new Import;
$imp->open($url);
$imp->setElement('elementName');
while ($data = $imp->parse()) {
foreach($data as $elementName) {
# SimpleXML object
print_r($elementName);
}
}
Příklad pro soubor.xml
<elementName>
<elm1>Content</elm1>
<elm2>Content</elm2>
</elementName>
<elementName>
<elm1>Content</elm1>
<elm2>Content</elm2>
</elementName>