XMLWriter is a standard XML API for result lists - just like a database result. It works in a linear way, keeping the memory usage low.
The XMLWriter takes care of the escaping as needed. Make sure that you have set the encoding of your database connection to utf-8.
$records = [
[
'questionid' => '1',
'item' => 'one'
],
[
'questionid' => '2',
'item' => 'two'
]
];
header('Content-type: text/xml; charset: utf-8');
$out = new XMLWriter();
$out->openURI('php://output');
$out->setIndent(true);
$out->startDocument('1.0', 'UTF-8');
$out->startElement('quiz');
while (list($key, $record) = each($records)) {
$out->startElement('question');
$out->writeAttribute('id', $record['questionid']);
$out->writeElement('item', $record['item']);
$out->endElement();
}
$out->endElement();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<question id="1">
<item>one</item>
</question>
<question id="2">
<item>two</item>
</question>
</quiz>