php 创建XML文档

巴扎黑
巴扎黑 原创
2016-11-22 16:35:24 697浏览
<?php
// 创建DOM
$dom = new DOMDocument("1.0", "UTF-8");
$dom -> formatOutput = true;
// 创建元素
$phpedu = $dom -> createElement("phpedu");
// 添加元素到父节点
$dom -> appendChild($phpedu);
$title = $dom -> createElement("title", "PHP100");
$phpedu -> appendChild($title);
// 创建元素
$item = $dom -> createElement("item");
// 设置属性
$item -> setAttribute("type", "text");
// 设置属性
$item -> setAttribute("name", "item");
// 添加元素到父节点
$phpedu -> appendChild($item);
$php = $dom -> createElement("contents", "PHP");
$php -> setAttribute("type", "text");
$item -> appendChild($php);
$sql = $dom -> createElement("contents", "SQL");
$item -> appendChild($sql);
$linux = $dom -> createElement("contents", "Linux");
$item -> appendChild($linux);
$apache = $dom -> createElement("contents", "Apache");
$item -> appendChild($apache);
$address = $dom -> createElement("Address", "www.php100.com");
$address -> setAttribute("type", "URL");
$phpedu -> appendChild($address);
echo htmlspecialchars($dom -> saveXML());
?>


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。