How can XML files be generated in PHP using the XMLWriter class?
To generate XML files in PHP using the XMLWriter class, you can create a new XMLWriter object, set the output to a file or string, and then use methods like startElement(), writeElement(), and endElement() to build the XML structure. Finally, you can use the flush() method to write the XML content to the output.
<?php
// Create a new XMLWriter object
$xml = new XMLWriter();
// Set the output to a file
$xml->openURI('output.xml');
// Start the document
$xml->startDocument('1.0', 'UTF-8');
// Start an element
$xml->startElement('root');
// Write elements
$xml->writeElement('element1', 'value1');
$xml->writeElement('element2', 'value2');
// End the element
$xml->endElement();
// End the document
$xml->endDocument();
// Flush the content to the output
$xml->flush();
?>