DOMDocument->createElement()

(no version information, might be only in CVS)

DOMDocument->createElement() -- 新しい要素ノードを作成する

説明

class DOMDocument {

DOMElement createElement ( string name [, string value] )

}

この関数は、DOMElement クラスの新しいインスタンスを作成します。 このノードは、DOMNode->appendChild() などで 挿入されない限り、ドキュメント内に現われません。

パラメータ

name

要素のタグ名。

value

要素の値。デフォルトでは、空の要素が作成されます。 その後に DOMElement->nodeValue で 値を設定することも可能です。

返り値

新しい DOMElement クラスの新しいインスタンス、 あるいはエラーが発生した場合は FALSE を返します。

エラー / 例外

DOM_INVALID_CHARACTER_ERR

name が無効な文字を含んでいる場合に発生します。

例 1. 新しい要素を作成し、ルートとして挿入する

<?php

$dom
= new DOMDocument('1.0', 'iso-8859-1');

$element = $dom->createElement('test', 'This is the root element!');

// 新しい要素をルート (ドキュメントの子要素) として挿入する
$dom->appendChild($element);

echo
$dom->saveXML();
?>

上の例の出力は以下となります。

<?xml version="1.0" encoding="iso-8859-1"?>
<test>This is the root element!</test>

参考

DOMNode->appendChild()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()