Класс DOMNode

(PHP 5)

Обзор классов

DOMNode {
/* Свойства */
public readonly string $nodeName ;
public string $nodeValue ;
public readonly int $nodeType ;
public readonly DOMNode $parentNode ;
public readonly DOMNodeList $childNodes ;
public readonly DOMNode $firstChild ;
public readonly DOMNode $lastChild ;
public readonly DOMNode $previousSibling ;
public readonly DOMNode $nextSibling ;
public readonly DOMNamedNodeMap $attributes ;
public readonly DOMDocument $ownerDocument ;
public readonly string $namespaceURI ;
public string $prefix ;
public readonly string $localName ;
public readonly string $baseURI ;
public readonly string $textContent ;
/* Методы */
public DOMNode appendChild ( DOMNode $newnode )
public string C14N ([ bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public int C14NFile ( string $uri [, bool $exclusive [, bool $with_comments [, array $xpath [, array $ns_prefixes ]]]] )
public DOMNode cloneNode ([ bool $deep ] )
public int getLineNo ( void )
public string getNodePath ( void )
public bool hasAttributes ( void )
public bool hasChildNodes ( void )
public DOMNode insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
public bool isDefaultNamespace ( string $namespaceURI )
public bool isSameNode ( DOMNode $node )
public bool isSupported ( string $feature , string $version )
public string lookupNamespaceURI ( string $prefix )
public string lookupPrefix ( string $namespaceURI )
public void normalize ( void )
public DOMNode removeChild ( DOMNode $oldnode )
public DOMNode replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

Свойства

nodeName

Возвращает имя текущего узла

nodeValue

Возращает значение текущего узла

nodeType

Возвращает тип текущего узла. Возможные варианты возвращаемых значений nodeType перечисленны в разделе констатнты XML_xxx_NODE

parentNode

Возвращает родительский узел текущего узла

childNodes

Возвращает список DOMNodeList, содержащий всех потомков текущего узла. Если у узла нет потомков, возвращает пустой список DOMNodeList.

firstChild

Возвращает первый дочерний узел текущего узла. Если у узла нет потомков, возвращает NULL.

lastChild

Возвращает последний дочерний узел текущего узла. Если у узла нет потомков, возвращает NULL.

previousSibling

Возвращает предыдущий узел непосредственно от текущего узла. Если такого узла нет, возвращает NULL.

nextSibling

Возвращает следующий узел после текущего узла. Если такого узла нет, возвращает NULL.

attributes

Возвращает объект DOMNamedNodeMap, содержащий атрибуты текущего узла (только если это DOMElement), иначе вернёт NULL.

ownerDocument

Возвращает корневой узел DOMDocument текущего узла.

namespaceURI

Вернёт пространство имён текущего узла или NULL если оно не установленно.

prefix

Вернёт префикс текущего узла или NULL если оно не установленно.

localName

Возвращает локальное имя текущего узла.

baseURI

Возвращает универсальный код ресурса (URI) текущего узела или NULL, если не удалось получить URI.

textContent

Этот атрибут возвращает текстовое содержимое текущего узла и его потомков

Примечания

Замечание:

Расширение DOM использует кодировку UTF-8. Используйте функции utf8_encode() и utf8_decode() для работы с текстами в кодировке ISO-8859-1 или функцию Iconv для других кодировок.

Содержание

Коментарии

This class has a getAttribute method.

Assume that a DOMNode object $ref contained an anchor taken out of a DOMNode List.  Then 

    $url = $ref->getAttribute('href'); 

would isolate the url associated with the href part of the anchor.
2008-12-08 01:27:01
http://php5.kiev.ua/manual/ru/class.domnode.html
And apparently also a setAttribute method too:

$node->setAttribute( 'attrName' , 'value' );
2009-04-06 08:39:17
http://php5.kiev.ua/manual/ru/class.domnode.html
It took me forever to find a mapping for the XML_*_NODE constants. So I thought, it'd be handy to paste it here:

 1 XML_ELEMENT_NODE
 2 XML_ATTRIBUTE_NODE
 3 XML_TEXT_NODE
 4 XML_CDATA_SECTION_NODE
 5 XML_ENTITY_REFERENCE_NODE
 6 XML_ENTITY_NODE
 7 XML_PROCESSING_INSTRUCTION_NODE
 8 XML_COMMENT_NODE
 9 XML_DOCUMENT_NODE
10 XML_DOCUMENT_TYPE_NODE
11 XML_DOCUMENT_FRAGMENT_NODE
12 XML_NOTATION_NODE
2009-05-05 11:36:07
http://php5.kiev.ua/manual/ru/class.domnode.html
Автор:
This class apparently also has a getElementsByTagName method.

I was able to confirm this by evaluating the output from DOMNodeList->item() against various tests with the is_a() function.
2009-11-03 13:47:18
http://php5.kiev.ua/manual/ru/class.domnode.html
Автор:
You cannot simply overwrite $textContent, to replace the text content of a DOMNode, as the missing readonly flag suggests. Instead you have to do something like this:

<?php

$node
->removeChild($node->firstChild);
$node->appendChild(new DOMText('new text content'));

?>

This example shows what happens:

<?php

$doc 
DOMDocument::loadXML('<node>old content</node>');
$node $doc->getElementsByTagName('node')->item(0);
echo 
"Content 1: ".$node->textContent."\n";

$node->textContent 'new content';
echo 
"Content 2: ".$node->textContent."\n";

$newText = new DOMText('new content');

$node->appendChild($newText);
echo 
"Content 3: ".$node->textContent."\n";

$node->removeChild($node->firstChild);
$node->appendChild($newText);
echo 
"Content 4: ".$node->textContent."\n";

?>

The output is:

Content 1: old content // starting content
Content 2: old content // trying to replace overwriting $node->textContent
Content 3: old contentnew content // simply appending the new text node
Content 4: new content // removing firstchild before appending the new text node

If you want to have a CDATA section, use this:

<?php
$doc 
DOMDocument::loadXML('<node>old content</node>');
$node $doc->getElementsByTagName('node')->item(0);
$node->removeChild($node->firstChild);
$newText $doc->createCDATASection('new cdata content');
$node->appendChild($newText);
echo 
"Content withCDATA: ".$doc->saveXML($node)."\n";
?>
2010-01-08 03:54:10
http://php5.kiev.ua/manual/ru/class.domnode.html
Автор:
For clarification:
The assumingly 'discoverd' by previous posters and seemingly undocumented methods (.getElementsByTagName and .getAttribute) on this class (DOMNode) are in fact methods of the class DOMElement, which inherits from DOMNode.

See: class.domelement
2010-01-13 11:03:27
http://php5.kiev.ua/manual/ru/class.domnode.html
Автор:
For a reference with more information about the XML DOM node types, see http://www.w3schools.com/dom/dom_nodetype.asp

(When using PHP DOMNode, these constants need to be prefaced with "XML_")
2010-04-19 17:43:31
http://php5.kiev.ua/manual/ru/class.domnode.html
Just discovered that node->nodeValue strips out all the tags
2011-03-20 08:10:50
http://php5.kiev.ua/manual/ru/class.domnode.html
The issues around mixed content took me some experimentation to remember, so I thought I'd add this note to save others time.

When your markup is something like: <div><p>First text.</p><ul><li><p>First bullet</p></li></ul></div>, you'll get XML_ELEMENT_NODEs that are quite regular. The <div> has children <p> and <ul> and the nodeValue for both <p>s yields the text you expect.

But when your markup is more like <p>This is <b>bold</b> and this is <i>italic</i>.</p>, you realize that the nodeValue for XML_ELEMENT_NODEs is not reliable. In this case, you need to look at the <p>'s child nodes. For this example, the <p> has children: #text, <b>, #text, <i>, #text. 

In this example, the nodeValue of <b> and <i> is the same as their #text children. But you could have markup like: <p>This <b>is bold and <i>bold italic</i></b>, you see?</p>. In this case, you need to look at the children of <b>, which will be #text, <i>, because the nodeValue of <b> will not be sufficient.

XML_TEXT_NODEs have no children and are always named '#text'. Depending on how whitespace is handled, your tree may have "empty" #text nodes as children of <body> and elsewhere.

Attributes are nodes, but I had forgotten that they are not in the tree expressed by childNodes. Walking the full tree using childNodes will not visit any attribute nodes.
2011-09-25 11:44:35
http://php5.kiev.ua/manual/ru/class.domnode.html
Here is a little function that truncates a DomNode to a specified number of text characters. I use it to generate HTML excerpts for my blog entries.

<?php

function makehtmlexcerpt(DomNode $html$excerptlength)
{
$remove 0;
$htmllength strlen(html_entity_decode($html->textContentENT_QUOTES'UTF-8'));
$truncate $htmllength $excerptlength;
if(
$htmllength $excerptlength)
{
if(
$html->hasChildNodes())
{
$children $html->childNodes;
for(
$counter 0$counter $children->length$counter ++)
{
$child $children->item($children->length - ($counter 1));
$childlength strlen(html_entity_decode($child->textContentENT_QUOTES'UTF-8'));
if(
$childlength <= $truncate)
{
$remove ++;
$truncate $truncate $childlength;
}
else
{
$child makehtmlexcerpt($child$childlength $truncate);
break;
}
}
if(
$remove != 0)
{
for(
$counter 0$counter $remove$counter ++)
{
$html->removeChild($html->lastChild);
}
}
}
else
{
if(
$html->nodeName == '#text')
{
$html->nodeValue substr(html_entity_decode($html->nodeValueENT_QUOTES'UTF-8'), 0$htmllength $truncate);
}
}
}
return 
$html;
}

?>
2013-08-29 16:15:15
http://php5.kiev.ua/manual/ru/class.domnode.html
In response to: alastair dot dallas at gmail dot com about "#text" nodes.
"#text" nodes appear when there are spaces or new lines between end tag and next initial tag.

Eg "<data><age>10</age>[SPACES]<other>20</other>[SPACES]</data>"

"data" childNodes has 4 childs:
- age = 10
- #text = spaces
- other = 20
- #text =  spaces
2014-04-16 17:36:07
http://php5.kiev.ua/manual/ru/class.domnode.html
Автор:
Yet another DOMNode to php array conversion function. 
Other ones on this page are generating too "complex" arrays; this one should keep the array as tidy as possible.
Note: make sure to set LIBXML_NOBLANKS when calling DOMDocument::load, loadXML or loadHTML
See: http://be2.php.net/manual/en/libxml.constants.php
See: http://be2.php.net/manual/en/domdocument.loadxml.php

<?php
         
/**
         * Returns an array representation of a DOMNode
         * Note, make sure to use the LIBXML_NOBLANKS flag when loading XML into the DOMDocument
         * @param DOMDocument $dom
         * @param DOMNode $node
         * @return array
         */
       
function nodeToArray$dom$node) {
            if(!
is_a$dom'DOMDocument' ) || !is_a$node'DOMNode' )) {
                return 
false;
            }
           
$array false
            if( empty( 
trim$node->localName ))) {// Discard empty nodes
               
return false;
            }
            if( 
XML_TEXT_NODE == $node->nodeType ) {
                return 
$node->nodeValue;
            }
            foreach (
$node->attributes as $attr) { 
               
$array['@'.$attr->localName] = $attr->nodeValue
            } 
            foreach (
$node->childNodes as $childNode) { 
                if ( 
== $childNode->childNodes->length && XML_TEXT_NODE == $childNode->firstChild->nodeType ) { 
                   
$array[$childNode->localName] = $childNode->nodeValue
                }  else {
                    if( 
false !== ($a self::nodeToArray$dom$childNode))) {
                       
$array[$childNode->localName] =     $a;
                    }
                }
            }
            return 
$array
        }
?>
2014-07-24 18:11:36
http://php5.kiev.ua/manual/ru/class.domnode.html
A function that can set the inner HTML without encoding error. $html can be broken content such as "<a ID=id20>ssss"
function setInnerHTML($node, $html) {
    removeChildren($node);
    if (empty($html)) {
        return;
    }
   
    $doc = $node->ownerDocument;
    $htmlclip = new DOMDocument();
    $htmlclip->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset=utf-8"><div>' . $html . '</div>');
    $clipNode = $doc->importNode($htmlclip->documentElement->lastChild->firstChild, true);
    while ($item = $clipNode->firstChild) {
        $node->appendChild($item);
    }
}
2016-01-15 15:07:05
http://php5.kiev.ua/manual/ru/class.domnode.html
Автор:
It would be helpful if docs for concrete properties mentioned readonly status of some properties:
"
ownerDocument

    The DOMDocument object associated with this node.

"
2018-05-06 19:12:29
http://php5.kiev.ua/manual/ru/class.domnode.html

    Поддержать сайт на родительском проекте КГБ