Documentation

XmlWriter

XmlWriter for export into XML

Method Description Available from version
construct Method - constructor.
writeItem Writes single item into XML file.
writeFullTag Writes full tag to file.
writeEndTag Writes tag end into file.
writeBeginTag Starts to write tag into file.
openFile Opens file for writing and initiate XML record.
getErrors Returns error.
closeFile Closes opened file.

Example

$export = new \Bitrix\Main\XmlWriter(array(
   'file' => '',
   'create_file' => true,
   'charset' => SITE_CHARSET,
   'lowercase' => true //convert to lowercase
));

//open the file
$export->openFile();

//frame array with tag
$export->writeBeginTag('items');

//get an item in the selection - note, that nesting is supported
$test = array(
   'name' => ' " \' Tra & tata \' "',
   'TimE' => time(),
   'bbbb' => '',
   'array' => array(
      'one' => 1,
      'two' => 2,
      'subarray' => array(
         't1' => 1,
         't2' => 2
      )
   )
);

//write tag (element above)
$export->writeItem($test, 'item');

//pass the framing tag for this array as second parameter. It is the same as 
$export->writeItem(array('item' =>  $test));

//get errors
//$export->getErrors()

//close array with tag
$export->writeEndTag('items');

//close the file
$export->closeFile();

Class convenience is in the sequential writing to file. For example, if you have export and you write a large array from database into file. Below is the updated earlier example for implementing sequential algorithm (differences are marked (***)).

$export =  new \Bitrix\Main\XmlWriter(array(
   'file' => '',
   'create_file' => $step == 0,//create file only on the first step (***)
   'charset' => SITE_CHARSET,
   'lowercase' => true //converting all tags to lowercase
));

//open the file
$export->openFile();

//frame array with tag (***)
if ($step == 0)
{
    $export->writeBeginTag('items');
}

//write the tag (element above) (***) - return each time on master hit
$export->writeItem($test, 'item');

//close the array with tag (***)
if ($step == 0)
{
    $export->writeEndTag('items');
}

//close the file
$export->closeFile();


© «Bitrix24», 2001-2024
Up