PHP Doku:: The DOMElement class - class.domelement.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMElement class

Ein Service von Reinhard Neidl - Webprogrammierung.

Document Object Model

<<The DOMDocumentType class

DOMElement::__construct>>


UnterSeiten:

The DOMElement class

Klassenbeschreibung

DOMElement extends DOMNode {
/* Eigenschaften */
readonly public bool $schemaTypeInfo ;
readonly public string $tagName ;
/* Methoden */
DOMElement::__construct ( string $name [, string $value [, string $namespaceURI ]] )
string DOMElement::getAttribute ( string $name )
DOMAttr DOMElement::getAttributeNode ( string $name )
DOMAttr DOMElement::getAttributeNodeNS ( string $namespaceURI , string $localName )
string DOMElement::getAttributeNS ( string $namespaceURI , string $localName )
DOMNodeList DOMElement::getElementsByTagName ( string $name )
DOMNodeList DOMElement::getElementsByTagNameNS ( string $namespaceURI , string $localName )
bool DOMElement::hasAttribute ( string $name )
bool DOMElement::hasAttributeNS ( string $namespaceURI , string $localName )
bool DOMElement::removeAttribute ( string $name )
bool DOMElement::removeAttributeNS ( string $namespaceURI , string $localName )
DOMAttr DOMElement::setAttribute ( string $name , string $value )
void DOMElement::setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )
void DOMElement::setIdAttribute ( string $name , bool $isId )
void DOMElement::setIdAttributeNode ( DOMAttr $attr , bool $isId )
void DOMElement::setIdAttributeNS ( string $namespaceURI , string $localName , bool $isId )
/* Geerbte Methoden */
DOMNode DOMNode::appendChild ( DOMNode $newnode )
DOMNode DOMNode::cloneNode ([ bool $deep ] )
public int DOMNode::getLineNo ( void )
bool DOMNode::hasAttributes ( void )
bool DOMNode::hasChildNodes ( void )
DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
bool DOMNode::isDefaultNamespace ( string $namespaceURI )
bool DOMNode::isSupported ( string $feature , string $version )
string DOMNode::lookupNamespaceURI ( string $prefix )
string DOMNode::lookupPrefix ( string $namespaceURI )
void DOMNode::normalize ( void )
DOMNode DOMNode::removeChild ( DOMNode $oldnode )
DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

Eigenschaften

schemaTypeInfo

Not implemented yet, always return NULL

tagName

The element name

Anmerkungen

Hinweis:

The DOM extension uses UTF-8 encoding. Use utf8_encode() and utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for other encodings.

Inhaltsverzeichnis


12 BenutzerBeiträge:
- Beiträge aktualisieren...
Anonymous
24.12.2010 17:22
you can use DOMNode::nodeValue
DOMElement inherits this public property.

$elem->nodeValue
dpetroff ( at ) gmail.com
5.12.2010 8:24
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;
}
?>
php at fishpig dot co dot uk
11.07.2010 18:53
A quick an easy way to get a string representation of any XML value is as follows:

<?php

 
echo $elem->property->__toString();

?>
martin at larsen dot dk
23.06.2010 12:09
This is a very simple way to get the innerHTML of a node:

<?php
function getNodeInnerHTML($elem) {

   return
simplexml_import_dom($elem)->asXML();

}
?>

I had trouble making other suggestions works, but this works perfectly for me.
adam dot clements at gmail dot com
26.04.2010 20:29
I spent ages figuring out how to get a string representation of a DOMElement (that wasn't the entire DOMDocument), finally I came across this snippet:

<?php $string = $domDocument->saveXML($node); ?>

where $domDocument is the entire document, and $node is the particular node/element/whatever you want a string XML representation of.
nawaman at gmail dot com
4.09.2009 5:08
The following code shows can text-only content be extracted from a document.

<?php
function getTextFromNode($Node, $Text = "") {
    if (
$Node->tagName == null)
        return
$Text.$Node->textContent;

   
$Node = $Node->firstChild;
    if (
$Node != null)
       
$Text = getTextFromNode($Node, $Text);

    while(
$Node->nextSibling != null) {
       
$Text = getTextFromNode($Node->nextSibling, $Text);
       
$Node = $Node->nextSibling;
    }
    return
$Text;
}

function
getTextFromDocument($DOMDoc) {
    return
getTextFromNode($DOMDoc->documentElement);
}

$Doc = new DOMDocument();
$Doc->loadHTMLFile("Test.html");
echo
getTextFromDocument($Doc)."\n";
?>
Daniel Morlock
7.05.2009 18:22
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();
}
?>
patrick smith
4.11.2008 17:14
Although it may be preferable to use the dom to manipulate elements, sometimes it's useful to actually get the innerHTML from a document element (e.g. to load into a client-side editor).

To get the innerHTML of a specific element ($elem_id) in a specific html file ($filepath):

<?php
$innerHTML
= '';
$doc = new DOMDocument();
$doc->loadHTMLFile($filepath);   
$elem = $doc->getElementById($elem_id);

// loop through all childNodes, getting html       
$children = $elem->childNodes;
foreach (
$children as $child) {
   
$tmp_doc = new DOMDocument();
   
$tmp_doc->appendChild($tmp_doc->importNode($child,true));       
   
$innerHTML .= $tmp_doc->saveHTML();
}
?>
Pinochet
25.10.2008 14:33
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 ;-)
j DOT wagner ( AT ) medieninnovation.com
8.10.2008 18:11
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.
Severin
14.09.2008 15:18
I wanted to find similar Elements - thats why I built an Xpath-String like this - maybe somebody needs it... its not very pretty - but neither is domdocument :)

<?php

$dom
->load($xmlFile))

$xpathQuery = '//*';
$xmlNodes = $xpath->query($xpathQuery);
       
$pathlist = array();
$attrlist = array();
foreach (
$xmlNodes as $node) {

 
$depth = $this->_getDomDepth($node);   //get Path-Depth (for array key)
 
$pathlist[$depth] = $node->tagName;     // tagname
         
 
$attrs = $node->attributes;
 
$attr='';
 
$a=0;
  foreach (
$attrs as $attrName => $attrNode// attributes
           
{
              if (
$attrName !='reg')
              {
                if (
$a++!=0) $attr .= ' and ';
               
$attr .= '@'.$attrName.'='."'".$attrNode->value."'";
              }
            }
         
         
$attrlist[$depth] = $attr?'['.$attr.']':'';
         
         
$path = ''; for ($i=0;$i<=$depth;$i++) $path .= '/'.$pathlist[$i].$attrlist[$i];  // the xpath of the actual Element

    // ... now you can go on and user $path to find similar elements
   
}
  }
}

 private function
_getDomDepth(DomNode $node)
   {
    
$r = -2;
     while (
$node) {
      
$r++; 
      
$node = $node->parentNode;
     }
     return 
$r;
   }
?>
ae.fxx
18.07.2008 22:49
Hi there.

Remember to append a DOMNode (or any of its descendants) to a DOMDocument __BEFORE__ you try to append a child to it.

I don't know why it has to be this way but it can't be done without it.

bye



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)