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();
}
?>
La classe DOMElement
Synopsis de la classe
DOMElement
DOMElement
étend
DOMNode
{
/* Propriétés */
/* Méthodes */
__construct
( string $name
[, string $value
[, string $namespaceURI
]] )
/* Méthodes héritées */
}Propriétés
- schemaTypeInfo
-
Non implémenté, retourne toujours NULL
- tagName
-
Le nom de l'élément
Sommaire
- DOMElement::__construct — Crée un nouvel objet DOMElement
- DOMElement::getAttribute — Retourne la valeur d'un attribut
- DOMElement::getAttributeNode — Retourne le noeud d'attribut
- DOMElement::getAttributeNodeNS — Retourne le noeud d'attribut
- DOMElement::getAttributeNS — Retourne la valeur de l'attribut
- DOMElement::getElementsByTagName — Retourne les éléments par leur nom de balise
- DOMElement::getElementsByTagNameNS — Récupère les éléments par leur espace de noms et leur localName
- DOMElement::hasAttribute — Vérifie si un attribut existe
- DOMElement::hasAttributeNS — Vérifie si un attribut existe
- DOMElement::removeAttribute — Efface un attribut
- DOMElement::removeAttributeNode — Efface un attribut
- DOMElement::removeAttributeNS — Efface un attribut
- DOMElement::setAttribute — Ajoute un nouvel attribut
- DOMElement::setAttributeNode — Ajoute un nouvel attribut à l'élément
- DOMElement::setAttributeNodeNS — Ajoute un nouvel attribut à l'élément
- DOMElement::setAttributeNS — Ajoute un nouvel attribut
- DOMElement::setIdAttribute — Déclare l'attribut spécifié par son nom à être de type ID
- DOMElement::setIdAttributeNode — Déclare l'attribut spécifié par le noeud à être de type ID
- DOMElement::setIdAttributeNS — Déclare l'attribut spécifié par son nom local et son espace de nom URI à être de type ID
DOMElement
patrick smith
04-Nov-2008 05:14
04-Nov-2008 05:14
Pinochet
25-Oct-2008 02:33
25-Oct-2008 02: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
08-Oct-2008 06:11
08-Oct-2008 06: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-Sep-2008 03:18
14-Sep-2008 03: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-Jul-2008 10:49
18-Jul-2008 10: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
