将XMLWriter输出到XML文件

时间:2011-03-14 18:40:47

标签: php xml xmlwriter

我目前正在使用XMLWriter来显示xml文件。但是,我想知道如何将输出导出到.xml文件。

我目前的代码是:

$res = mysql_query($sql);

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument();
$xml->startElement('stores');

while ($row = mysql_fetch_assoc($res)) {
//loads of code
}
$xml->endElement();

$xml->flush();

1 个答案:

答案 0 :(得分:21)

php://output方法中使用文件名而不是openURI()

$writer = new XMLWriter();
$writer->openURI('test.xml');
$writer->startDocument("1.0");
$writer->startElement("greeting");
$writer->text('Hello World');
$writer->endDocument();
$writer->flush();