DOMElement クラス
(PHP 5, PHP 7, PHP 8)
クラス概要
/* プロパティ */
/* 継承したプロパティ */
/* メソッド */
/* 継承したメソッド */
public DOMNode::C14N(
bool
bool
?array
?array
): string|false
}bool
$exclusive
= false
,bool
$withComments
= false
,?array
$xpath
= null
,?array
$nsPrefixes
= null
): string|false
プロパティ
- childElementCount
-
子要素の数。
- firstElementChild
-
最初の子要素。存在しない場合は
null
になります。 - lastElementChild
-
最後の子要素。存在しない場合は
null
になります。 - nextElementSibling
-
次の兄弟要素。存在しない場合は
null
になります。 - previousElementSibling
-
前の兄弟要素。存在しない場合は
null
になります。 - schemaTypeInfo
-
未実装。常に
null
を返す。 - tagName
-
要素名
- className
-
スペースで分割された、要素のクラスを表す文字列
- id
-
要素のID
変更履歴
バージョン | 説明 |
---|---|
8.0.0 | プロパティ firstElementChild, lastElementChild, childElementCount, previousElementSibling, nextElementSibling が追加されました。 |
8.0.0 | DOMElement は、 DOMParentNode と DOMChildNode を実装しました。 |
注意
注意:
DOM拡張モジュール は UTF-8 エンコーディングを使います。他のエンコーディングを扱う場合は、mb_convert_encoding(), UConverter::transcode(), iconv() を使ってください。
目次
- DOMElement::after — 要素の後ろにノードを追加する
- DOMElement::append — 最後のノードの後ろにノードを追加する
- DOMElement::before — 要素の前にノードを追加する
- DOMElement::__construct — 新しい DOMElement オブジェクトを作成する
- DOMElement::getAttribute — 属性の値を返す
- DOMElement::getAttributeNames — 属性の名前を取得する
- DOMElement::getAttributeNode — 属性ノードを返す
- DOMElement::getAttributeNodeNS — 属性ノードを返す
- DOMElement::getAttributeNS — 属性の値を返す
- DOMElement::getElementsByTagName — タグ名から要素を取得する
- DOMElement::getElementsByTagNameNS — 名前空間 URI とローカル名から要素を取得する
- DOMElement::hasAttribute — 属性が存在するかどうかを調べる
- DOMElement::hasAttributeNS — 属性が存在するかどうかを調べる
- DOMElement::insertAdjacentElement — 隣接する要素を挿入する
- DOMElement::insertAdjacentText — 隣接するテキストを挿入する
- DOMElement::prepend — 最初の子ノードの前にノードを追加する
- DOMElement::remove — 要素を削除する
- DOMElement::removeAttribute — 属性を削除する
- DOMElement::removeAttributeNode — 属性を削除する
- DOMElement::removeAttributeNS — 属性を削除する
- DOMElement::replaceChildren — 要素中の子を置換する
- DOMElement::replaceWith — 要素を新しいノードで置換する
- DOMElement::setAttribute — 属性を新しく追加する、または変更する
- DOMElement::setAttributeNode — 新しい属性ノードを要素に追加する
- DOMElement::setAttributeNodeNS — 新しい属性ノードを要素に追加する
- DOMElement::setAttributeNS — 新しい属性を追加する
- DOMElement::setIdAttribute — ID 型の属性を名前で宣言する
- DOMElement::setIdAttributeNode — ID 型の属性をノードで宣言する
- DOMElement::setIdAttributeNS — ID 型の属性をローカル名および名前空間 URI で宣言する
- DOMElement::toggleAttribute — 属性の値を切り替える
+add a note
User Contributed Notes 13 notes
j DOT wagner ( AT ) medieninnovation.com ¶
16 years ago
Caveat!
It took me almost an hour to figure this out, so I hope it saves at least one of you some time.
If you want to debug your DOM tree and try var_dump() or similar you will be fooled into thinking the DOMElement that you are looking at is empty, because var_dump() says: object(DOMElement)#1 (0) { }
After much debugging I found out that all DOM objects are invisible to var_dump() and print_r(), my guess is because they are C objects and not PHP objects. So I tried saveXML(), which works fine on DOMDocument, but is not implemented on DOMElement.
The solution is simple (if you know it):
$xml = $domElement->ownerDocument->saveXML($domElement);
This will give you an XML representation of $domElement.
Pinochet ¶
15 years ago
Hi to get the value of DOMElement just get the nodeValue public parameter (it is inherited from DOMNode):
<?php
echo $domElement->nodeValue;
?>
Everything is obvious if you now about this thing ;-)
Janne Enberg ¶
10 years ago
This page doesn't list the inherited properties from DOMNode, e.g. the quite important textContent property. It would be immensely helpful if it would list those as well.
dpetroff ( at ) gmail.com ¶
13 years ago
Hi!
Combining all th comments, the easiest way to get inner HTML of the node is to use this function:
<?php
function get_inner_html( $node ) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML;
}
?>
Daniel Morlock ¶
15 years ago
It would be nice to have a function which converts a document/node/element into a string.
Anyways, I use the following code snippet to get the innerHTML value of a DOMNode:
<?php
function getInnerHTML($Node)
{
$Body = $Node->ownerDocument->documentElement->firstChild->firstChild;
$Document = new DOMDocument();
$Document->appendChild($Document->importNode($Body,true));
return $Document->saveHTML();
}
?>
felix dot klee at inka dot de ¶
11 years ago
How to rename an element and preserve attributes:
<?php
// Changes the name of element $element to $newName.
function renameElement($element, $newName) {
$newElement = $element->ownerDocument->createElement($newName);
$parentElement = $element->parentNode;
$parentElement->insertBefore($newElement, $element);
$childNodes = $element->childNodes;
while ($childNodes->length > 0) {
$newElement->appendChild($childNodes->item(0));
}
$attributes = $element->attributes;
while ($attributes->length > 0) {
$attribute = $attributes->item(0);
if (!is_null($attribute->namespaceURI)) {
$newElement->setAttributeNS('http://www.w3.org/2000/xmlns/',
'xmlns:'.$attribute->prefix,
$attribute->namespaceURI);
}
$newElement->setAttributeNode($attribute);
}
$parentElement->removeChild($element);
}
function prettyPrint($d) {
$d->formatOutput = true;
echo '<pre>'.htmlspecialchars($d->saveXML()).'</pre>';
}
$d = new DOMDocument( '1.0' );
$d->loadXML('<?xml version="1.0"?>
<library>
<data a:foo="1" x="bar" xmlns:a="http://example.com/a">
<invite>
<username>jmansa</username>
<userid>1</userid>
</invite>
<update>1</update>
</data>
</library>');
$xpath = new DOMXPath($d);
$elements = $xpath->query('/library/data');
if ($elements->length == 1) {
$element = $elements->item(0);
renameElement($element, 'invites');
}
prettyPrint($d);
?>