Movatterモバイル変換


[0]ホーム

URL:


    DOMNode::appendChild »
    « DOMNameSpaceNode::__wakeup

    The DOMNode class

    (PHP 5, PHP 7, PHP 8)

    Class synopsis

    classDOMNode {
    /* Constants */
    /* Properties */
    publicreadonlystring$nodeName;
    publicreadonlyint$nodeType;
    publicreadonly?DOMNode$parentNode;
    publicreadonly?DOMElement$parentElement;
    publicreadonlyDOMNodeList$childNodes;
    publicreadonly?DOMNode$firstChild;
    publicreadonly?DOMNode$lastChild;
    publicreadonly?DOMNode$previousSibling;
    publicreadonly?DOMNode$nextSibling;
    publicreadonlybool$isConnected;
    publicreadonly?string$namespaceURI;
    publicreadonly?string$localName;
    publicreadonly?string$baseURI;
    /* Methods */
    publicC14N(
        bool$exclusive =false,
        bool$withComments =false,
        ?array$xpath =null,
        ?array$nsPrefixes =null
    ):string|false
    publicC14NFile(
        string$uri,
        bool$exclusive =false,
        bool$withComments =false,
        ?array$xpath =null,
        ?array$nsPrefixes =null
    ):int|false
    publicgetLineNo():int
    publicgetRootNode(?array$options =null):DOMNode
    publicisDefaultNamespace(string$namespace):bool
    publicisEqualNode(?DOMNode$otherNode):bool
    publicisSameNode(DOMNode$otherNode):bool
    publicisSupported(string$feature,string$version):bool
    publiclookupPrefix(string$namespace):?string
    publicnormalize():void
    public__sleep():array
    public__wakeup():void
    }

    Predefined Constants

    DOMNode::DOCUMENT_POSITION_DISCONNECTED
    Set when the other node and reference node are not in the same tree.
    DOMNode::DOCUMENT_POSITION_PRECEDING
    Set when the other node precedes the reference node.
    DOMNode::DOCUMENT_POSITION_FOLLOWING
    Set when the other node follows the reference node.
    DOMNode::DOCUMENT_POSITION_CONTAINS
    Set when the other node is an ancestor of the reference node.
    DOMNode::DOCUMENT_POSITION_CONTAINED_BY
    Set when the other node is a descendant of the reference node.
    DOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
    Set when the result depends on implementation-specific behaviour and may not be portable. This may happen with disconnected nodes or with attribute nodes.

    Properties

    nodeName

    Returns the most accurate name for the current node type

    nodeValue

    The value of this node, depending on its type. Contrary to the W3C specification, the node value ofDOMElement nodes is equal toDOMNode::textContent instead ofnull.

    nodeType

    Gets the type of the node. One of the predefinedXML_*_NODE constants

    parentNode

    The parent of this node. If there is no such node, this returnsnull.

    parentElement

    The parent element of this element. If there is no such element, this returnsnull.

    childNodes

    ADOMNodeList that contains all children of this node. If there are no children, this is an emptyDOMNodeList.

    firstChild

    The first child of this node. If there is no such node, this returnsnull.

    lastChild

    The last child of this node. If there is no such node, this returnsnull.

    previousSibling

    The node immediately preceding this node. If there is no such node, this returnsnull.

    nextSibling

    The node immediately following this node. If there is no such node, this returnsnull.

    attributes

    ADOMNamedNodeMap containing the attributes of this node (if it is aDOMElement) ornull otherwise.

    isConnected

    Whether the node is connected to a document

    ownerDocument

    TheDOMDocument object associated with this node, ornull if this node does not have an associated document (e.g. if it is detached, or if it is aDOMDocument).

    namespaceURI

    The namespace URI of this node, ornull if it is unspecified.

    prefix

    The namespace prefix of this node.

    localName

    Returns the local part of the qualified name of this node.

    baseURI

    The absolute base URI of this node ornull if the implementation wasn't able to obtain an absolute URI.

    textContent

    The text content of this node and its descendants.

    Changelog

    VersionDescription
    8.4.0 MethodDOMNode::compareDocumentPosition() has been added.
    8.4.0 ConstantsDOMNode::DOCUMENT_POSITION_DISCONNECTED,DOMNode::DOCUMENT_POSITION_PRECEDING,DOMNode::DOCUMENT_POSITION_FOLLOWING,DOMNode::DOCUMENT_POSITION_CONTAINS,DOMNode::DOCUMENT_POSITION_CONTAINED_BY, andDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC have been added.
    8.3.0 MethodsDOMNode::contains(), andDOMNode::isEqualNode() have been added.
    8.3.0 PropertiesDOMNode::$parentElement, andDOMNode::$isConnected have been added.
    8.0.0 The unimplemented methodsDOMNode::compareDocumentPosition(),DOMNode::isEqualNode(),DOMNode::getFeature(),DOMNode::setUserData() andDOMNode::getUserData() have been removed.

    Notes

    Note:

    The DOM extension uses UTF-8 encoding. Usemb_convert_encoding(),UConverter::transcode(), oriconv() to handle other encodings.

    See Also

    Table of Contents

    Found A Problem?

    Learn How To Improve This PageSubmit a Pull RequestReport a Bug
    add a note

    User Contributed Notes13 notes

    marc at ermshaus dot org
    16 years ago
    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_NODE10 XML_DOCUMENT_TYPE_NODE11 XML_DOCUMENT_FRAGMENT_NODE12 XML_NOTATION_NODE
    David Rekowski
    15 years ago
    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(newDOMText('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= newDOMText('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 contentContent 2: old content // trying to replace overwriting $node->textContentContent 3: old contentnew content // simply appending the new text nodeContent 4: new content // removing firstchild before appending the new text nodeIf 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";?>
    R. Studer
    15 years ago
    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:http://www.php.net/manual/en/class.domelement.php
    brian wildwoodassociates.info
    16 years ago
    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.
    alastair dot dallas at gmail dot com
    14 years ago
    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.
    pizarropablo at gmail dot com
    11 years ago
    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
    imranomar at gmail dot com
    14 years ago
    Just discovered that node->nodeValue strips out all the tags
    Steve K
    16 years ago
    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.
    metanull
    11 years ago
    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 loadHTMLSee:http://be2.php.net/manual/en/libxml.constants.phpSee: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         */functionnodeToArray($dom,$node) {            if(!is_a($dom,'DOMDocument') || !is_a($node,'DOMNode')) {                returnfalse;            }$array=false;             if( empty(trim($node->localName))) {// Discard empty nodesreturnfalse;            }            if(XML_TEXT_NODE==$node->nodeType) {                return$node->nodeValue;            }            foreach ($node->attributesas$attr) {$array['@'.$attr->localName] =$attr->nodeValue;             }             foreach ($node->childNodesas$childNode) {                 if (1==$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;         }?>
    matej dot golian at gmail dot com
    12 years ago
    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.<?phpfunctionmakehtmlexcerpt(DomNode $html,$excerptlength){$remove=0;$htmllength=strlen(html_entity_decode($html->textContent,ENT_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->textContent,ENT_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->nodeValue,ENT_QUOTES,'UTF-8'),0,$htmllength-$truncate);}}}return$html;}?>
    matt at lamplightdb dot co dot uk
    16 years ago
    And apparently also a setAttribute method too:$node->setAttribute( 'attrName' , 'value' );
    Anonymous
    7 years ago
    It would be helpful if docs for concrete properties mentioned readonly status of some properties:"ownerDocument    The DOMDocument object associated with this node."
    zlk1214 at gmail dot com
    9 years ago
    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);    }}
    add a note
    To Top
    and to navigate •Enter to select •Esc to close •/ to open
    PressEnter without selection to search using Google

    [8]ページ先頭

    ©2009-2025 Movatter.jp