If you have empty $node->textContent and $node->textValue, check if document that is loaded have UTF-8 encoding.
La clase DOMNode
Clases sinopsis
Propiedades
- nodeName
-
Devuelve el nombre más exacto del tipo de nodo actual
- nodeValue
-
El valor de este nodo, dependiendo de su tipo
- nodeType
-
Obtiene el tipo del nodo. Una de las constantes XML_xxx_NODE predefinidas
- parentNode
-
El padre de este nodo
- childNodes
-
Un objeto DOMNodeList que tiene todos los hijos de este nodo. Si no hubiera hijos, es un objeto DOMNodeList vacío.
- firstChild
-
El primer hijo de este nodo. Si no existiera tal nodo devuelve NULL.
- lastChild
-
El último hijo de este nodo. Si no existiera tal nodo devuelve NULL.
- previousSibling
-
El nodo que precede inmediatamente a este ndo. Si no existiera tal nodo devuelve NULL.
- nextSibling
-
El nodo inmediatamente siguiente a este nodo. Si no existiera tal nodo devuelve NULL.
- attributes
-
Un objeto DOMNamedNodeMap que contiene los atributos de este nodo (si es un objeto DOMElement) o NULL de otro modo.
- ownerDocument
-
El objeto DOMDocument asociado con este nodo.
- namespaceURI
-
La URI del espacio de nombres de este nodo, o NULL si no está especificado.
- prefix
-
El prefijo del espacio de nombres de este nodo, o NULL si no está especificado.
- localName
-
Devuelve la parte local del nombre cualificado de este nodo.
- baseURI
-
La URI base absoluta de este nodo o NULL si la implementación no fue capaz de obtener una URI absoluta.
- textContent
-
Este atributo devuelve el contenido de texto de este nodo y sus descendientes.
Notas
Note:
La extensión DOM utiliza la codificación UTF-8. Use utf8_encode() y utf8_decode() para trabajar con textos con codificación ISO-8859-1 o Iconv para otras codificaciones.
Table of Contents
- DOMNode::appendChild — Añade un nuevo hijo al final de los hijos
- DOMNode::cloneNode — Clona un nodo
- DOMNode::getLineNo — Obtener el número de línea de un nodo
- DOMNode::hasAttributes — Comprueba si un nodo tiene atributos
- DOMNode::hasChildNodes — Comprueba si el nodo tiene hijos
- DOMNode::insertBefore — Añade un nuevo hijo antes del nodo de referencia
- DOMNode::isDefaultNamespace — Comprueba si la URI del espacio de nombres especificada es el espacio de nombres predeterminado
- DOMNode::isSameNode — Indica si dos nodos son el mismo nodo
- DOMNode::isSupported — Comprueba si una característica está soportada para la versión especificada
- DOMNode::lookupNamespaceURI — Obtiene la URI del espacio de nombres del nodo basado en el prefijo
- DOMNode::lookupPrefix — Obtiene el prefijo del espacio de nombres del nodo basándoes en la URI del espacio de nombres
- DOMNode::normalize — Normaliza el nodo
- DOMNode::removeChild — Elimina un hijo de la lista de hijos.
- DOMNode::replaceChild — Reemplaza un hijo
Just discovered that node->nodeValue strips out all the tags
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_")
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
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";
?>
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.
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
And apparently also a setAttribute method too:
$node->setAttribute( 'attrName' , 'value' );
Try canonicalization:
<?php
$dom = new DOMDocument;
$dom->loadHTMLFile('http://www.example.com/');
echo $dom->documentElement->C14N();
?>
Or output it to a file, using C14NFile()
Undocumented stuff ;)
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.
