生成xml的php代码:
<?php
echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';
$array = array(array('name'=>'php', 'type'=>'脚本语言'), array('name'=>'html', 'type'=>'标记语言', array('name'=>'c#', 'type'=>'动态语言')));
//设置版本号和字符编码
$doc = new DOMDocument("1.0", "utf-8");
//格式化输出
$doc->formatOutput=true;
//创建一个元素节点
$books = $doc->createElement('books');
foreach ($array as $value){
$book = $doc->createElement('book');
$books->appendChild($book);
//创建name节点并赋值
$name = $doc->createElement('name', $value['name']);
$book->appendChild($name);
//为book节点添加属性
$book->setAttribute('type', $value['type']);
}
$doc->appendChild($books);
$doc->save('books.xml');
echo '写入成功!';
生成的xml文档: