Skip to content

Instantly share code, notes, and snippets.

@nizom333
Last active July 31, 2022 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nizom333/0e84e90627e01401f7d7a790b58b6c9d to your computer and use it in GitHub Desktop.
Save nizom333/0e84e90627e01401f7d7a790b58b6c9d to your computer and use it in GitHub Desktop.
XmlWriter PHP
<?php
class XmlWriter
{
private $file = '';
private $charset = '';
private $tab = 0;
private $f = null;
private $lowercaseTag = false;
private $errors = array();
/**
* Constructor.
* @param array $params Array of settings.
*/
public function __construct(array $params)
{
if (isset($params['file']))
{
$server = \Bitrix\Main\Application::getInstance()->getContext()->getServer();
$this->file = $server->getDocumentRoot() . trim($params['file']);
// create new file
if (
isset($params['create_file']) &&
$params['create_file'] === true &&
is_writable($this->file)
)
{
unlink($this->file);
}
}
if (isset($params['charset']))
{
$this->charset = trim($params['charset']);
}
else
{
$this->charset = SITE_CHARSET;
}
if (isset($params['lowercase']) && $params['lowercase'] === true)
{
$this->lowercaseTag = true;
}
if (isset($params['tab']))
{
$this->tab = (int)$params['tab'];
}
}
/**
* Prepare tag for write.
* @param string $tag Code of tag.
* @return string
*/
private function prepareTag($tag)
{
if ($this->lowercaseTag)
{
$tag = mb_strtolower($tag);
}
return $tag;
}
/**
* Prepare value for write.
* @param string $value Value.
* @return string
*/
private function prepareValue($value)
{
$value = strtr(
$value,
array(
'<' => '&lt;',
'>' => '&gt;',
'"' => '&quot;',
'\'' => '&apos;',
'&' => '&amp;',
)
);
$value = preg_replace('/[\x01-\x08\x0B-\x0C\x0E-\x1F]/', '', $value);
return $value;
}
/**
* Write begin tag to file.
* @param string $code Code of tag.
* @return void
*/
public function writeBeginTag($code)
{
if ($this->f)
{
fwrite($this->f, str_repeat("\t", $this->tab) . '<' . $this->prepareTag($code) . '>' . PHP_EOL);
$this->tab++;
}
}
/**
* Write end tag to file.
* @param string $code Code of tag.
* @return void
*/
public function writeEndTag($code)
{
if ($this->f)
{
$this->tab--;
fwrite($this->f, str_repeat("\t", $this->tab) . '</' . $this->prepareTag($code) . '>' . PHP_EOL);
}
}
/**
* Write full tag to file.
* @param string $code Code of tag.
* @param string $value Code for tag.
* @return void
*/
public function writeFullTag($code, $value)
{
if ($this->f)
{
$code = $this->prepareTag($code);
fwrite($this->f,
str_repeat("\t", $this->tab) .
(
trim($value) == ''
? '<' . $code . ' />' . PHP_EOL
: '<' . $code . '>' .
$this->prepareValue($value) .
'</' . $code . '>' . PHP_EOL
)
);
}
}
/**
* Add one more error.
* @param mixed $message Text of error.
* @param mixed $code Code of error.
* @return void
*/
private function addError($message, $code)
{
$this->errors[] = new Error($message, $code);
}
/**
* Return errors if exists.
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Open file for write, start write xml.
* @return void
*/
public function openFile()
{
if ($this->file == '')
{
$this->addError('File not accessible.', 'XML_FILE_NOT_ACCESSIBLE');
}
else
{
\CheckDirPath($this->file);
$newFile = !file_exists($this->file);
if (file_exists($this->file) && !is_writable($this->file))
{
chmod($this->file, BX_FILE_PERMISSIONS);
}
if (($this->f = fopen($this->file, 'ab')))
{
chmod($this->file, BX_FILE_PERMISSIONS);
if ($newFile)
{
fwrite($this->f, '<?xml version="1.0" encoding="'. $this->charset .'"?>' . PHP_EOL);
}
}
else
{
$this->addError('File not accessible.', 'XML_FILE_NOT_ACCESSIBLE');
}
}
}
/**
* Close the open file.
* @return void
*/
public function closeFile()
{
if ($this->f)
{
fclose($this->f);
}
}
/**
* Write one chunk in xml file.
* @param array $item Data for write.
* @param string $wrapperTag If != '' wrapper the array in this tag.
* @return void
*/
public function writeItem(array $item, $wrapperTag = '')
{
if ($wrapperTag != '')
{
$this->writeBeginTag($wrapperTag);
}
foreach ($item as $tag => $value)
{
if (is_array($value))
{
$this->writeItem($value, $tag);
}
else
{
$this->writeFullTag($tag, $value);
}
}
if ($wrapperTag != '')
{
$this->writeEndTag($wrapperTag);
}
}
public function writeValue($value)
{
if ($this->f)
{
fwrite($this->f,
str_repeat("\t", $this->tab) . $this->prepareValue($value) . PHP_EOL
);
}
}
}
@nizom333
Copy link
Author

nizom333 commented Jun 30, 2021

// Use

$export = new XmlWriter(
    [
        'file' => '/fbfeed/products.xml', // относительный путь к создаваемому файлу, файл отсутствует и установлен параметр 'create_file', то он будет автоматически создан
        'create_file' => true, //создавать ли файл, или продолжить запись в уже созданный. В данном случае каждый раз будет создаваться и перезаписываться новый файл
        'charset' => 'UTF-8', //кодировка файла
        'lowercase' => false //приводить ли все теги к нижнему регистру
    ]
);
$export->openFile();

$export->writeBeginTag('listings');

    $export->writeItem([
        'title' => 'Catalog',
    ]);


        $export->writeBeginTag('listing');

            $export->writeItem(
                [
                    'home_listing_id' => $arFields['ID'],
                    'name' => $arFields['NAME'],
                    'availability' => 'for_sale',
                    'description' => strip_tags($arFields['DETAIL_TEXT']),
                    'image' => [
                        'url' => "https://work-space.uz{$img}",
                    ],
                    'price' => str_replace(['&amp;', '&nbsp;'], '', $price),
                    'url' => "https://work-space.uz" . $arFields['DETAIL_PAGE_URL'],
                ]
            );
            $export->writeBeginTag('address');
                $export->writeBeginTag('component name="country"');
                    $export->writeValue('Uzbekistan');
                $export->writeEndTag('component');
                $export->writeBeginTag('component name="city"');
                    $export->writeValue('Tashkent');
                $export->writeEndTag('component');
                $export->writeBeginTag('component name="region"');
                    $export->writeValue('');
                $export->writeEndTag('component');
            $export->writeEndTag('address');


        $export->writeEndTag('listing');


$export->writeEndTag('listings');


$export->closeFile();


echo '<pre>'; print_r($export->getErrors()); echo '</pre>';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment