没有合适的资源?快使用搜索试试~ 我知道了~
首页PHP 数组与Xml转换
资源详情
资源评论
资源推荐

Array 与 Xml
PHP 中数组生成 Xml。
//实例化 DOM 文档 DOMDocument(‘1.0’,‘utf-8’)。分别为版本号和字符
集
$dom = new DOMDocument('1.0', 'utf-8');
//输出的模式
$dom->formatOutput = true;
//创建整个 xml 的总标签
$booksElement = $dom->createElement('books');
//$books 为要生成 xml 的数组
foreach ($books as $no => $book)
{
//第一个子元素即第一个模块信息
$bookElement = $dom->createElement('book');
//创建第一个模块信息的子元素及信息
$authorElement = $dom->createElement('author');
//内容
$authorElement->appendChild($dom-
>createTextNode($book['author']));
$titleElement = $dom->createElement('title');
$titleElement->appendChild($dom->createTextNode($book['title']));
$introduceElement = $dom->createElement('introduce');
$introduceElement->appendChild($dom-
>createTextNode($book['introduce']));
//即整个模块的信息赋予信息
$bookElement->appendChild($authorElement);
$bookElement->appendChild($titleElement);
$bookElement->appendChild($introduceElement);
//设置属性
$bookElement->setAttribute('no',$no);
//将所有模块信息赋予最外层的 books
$booksElement->appendChild($bookElement);
}
$dom->appendChild($booksElement);
//生成 xml 文件
$dom->save('book1.xml');
以上显示代码为数组单个生成,即数组有多少个元素就要有多少相应的代码。


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论5