2010-11-17 112 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());