2011-03-14 116 views
10

我目前使用XMLWriter來顯示一個xml文件。不過,我想知道如何將輸出導出到.xml文件。輸出XMLWriter到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(); 

回答

20

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

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

你知道如果有可能創建目錄,如果它不存在? – 2014-12-15 05:17:20

+3

@Mcio您可以隨時使用http://php.net/mkdir – Gordon 2014-12-15 06:39:41