<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
通过使用属性,你也可以添加适合的信息到元素。对于PHP DOM API,添加属性需要两步:首先用DOM文档对象的createAttribute()方法创建拥有此属性名字的节点,然后将文档节点添加到拥有属性值的属性节点。详见程序清单D。
程序清单 D
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);