2010-11-17 118 views
0

如何使用PHP創建此類型的XML輸出?在PHP中創建輸出XML文件

<Root> 
<base> rushi </base> 
<base> stack </base> 
<base> overflow </base> 
<root> 
+0

你添加一個'?>'標記並且只是把它寫下來。請更具體的你正在尋找什麼樣的解決方案。在旁註中,你的XML是無效的。根節點未關閉。 XML區分大小寫。 – Gordon 2010-11-17 08:00:53

+0

相關和可能的重複:[什麼是在PHP中使用XML的最佳方法](http://stackoverflow.com/questions/2060346/php-what-is-the-best-approach-to-using-xml-need -to-create-and-parse-xml-response) – Gordon 2010-11-17 08:03:37

+0

*(reference)* http://de.php.net/manual/en/refs.xml.php – Gordon 2010-11-17 08:11:07

回答

1
$employees = array(
    '0'=> array(
     'name' =>'abin', 
      'age' => '23', 
    ), 
    '1' => array(
     'name' => 'savin', 
     'age' => '24', 
    ) 
); 
//$file = fopen('file.xml','w'); 
$xml = new SimpleXMLElement('<employees></employees>'); 
foreach($employees as $employ): 
$xml->addChild('name',$employ['name']); 
$xml->addChild('age',$employ['age']); 
endforeach; 
file_put_contents('file.xml',$xml->saveXML());