Create an XML file using XMLWriter

suggest change

Instantiate a XMLWriter object:

$xml = new XMLWriter();

Next open the file to which you want to write. For example, to write to /var/www/example.com/xml/output.xml, use:

$xml->openUri('file:///var/www/example.com/xml/output.xml');

To start the document (create the XML open tag):

$xml->startDocument('1.0', 'utf-8');

This will output:

<?xml version="1.0" encoding="UTF-8"?>

Now you can start writing elements:

$xml->writeElement('foo', 'bar');

This will generate the XML:

<foo>bar</foo>

If you need something a little more complex than simply nodes with plain values, you can also “start” an element and add attributes to it before closing it:

$xml->startElement('foo');
$xml->writeAttribute('bar', 'baz');
$xml->writeCdata('Lorem ipsum');
$xml->endElement();

This will output:

<foo bar="baz"><![CDATA[Lorem ipsum]]></foo>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


XML:
* XML
* Create an XML file using XMLWriter

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR