如果說明不夠清楚(就像我之前一樣),這裡提供一個範例
<?php
$domDocument = new DOMDocument('1.0', "UTF-8");
$domElement = $domDocument->createElement('field','一些隨機資料');
$domAttribute = $domDocument->createAttribute('name');
// 已建立屬性的值
$domAttribute->value = 'attributevalue';
// 別忘了將它附加到元素
$domElement->appendChild($domAttribute);
// 將它附加到文件本身
$domDocument->appendChild($domElement);
?>
將會輸出
<?xml version="1.0" encoding="UTF-8"?>
<field name="attributevalue">一些隨機資料</field>