In a numbered ordering of the document's nodes, insertBefore() will place newNode at the index held by refNode, and increment refNode's index and all subsequent nodes' indices. This is instead of maintaining refNode's index, placing newNode at refNode's position minus one, and shifting all previous nodes' indices down by one.
The base case of refNode.index = 0 demonstrates why this must be the case, but it is good to know this explicitly, as it affects methods that deal with iteration such as getElementsByTagName().
DomNode->insert_before
(No version information available, might be only in CVS)
DomNode->insert_before — Inserta un nodo nuevo como hijo
Descripción
Esta función inserta el nuevo nodo, nodo_nuevo , justo antes del nodo nodo_ref . El valor de retorno es el nodo insertado. Si planea hacer modificaciones posteriores sobre el hijo agregado, debe usar el nodo devuelto.
(Sólo PHP >= 4.3) Si nodo_nuevo ya es parte de un documento, será primero desenlazado de su contexto actual. Si nodo_ref es NULL, entonces nodo_nuevo será insertado al final de la lista de hijos.
domnode_insert_before() es bastante similar a domnode_append_child() como muestra el siguiente ejemplo, el cual hace lo mismo que el ejemplo en domnode_append_child().
Example #1 Agregar un hijo
<?php
include("ejemplo.inc");
if (!$dom = domxml_open_mem($cadena_xml)) {
echo "Ocurrió un error al analizar el documento\n";
exit;
}
$elementos = $dom->get_elements_by_tagname("informaltable");
print_r($elementos);
$elemento = $elementos[0];
$nodo_nuevo = $elemento->insert_before($elemento, $elemento);
$hijos = $nodo_nuevo->children();
$atr = $hijos[1]->set_attribute("align", "left");
echo "<pre>";
$archivo_xml = $dom->dump_mem();
echo htmlentities($archivo_xml);
echo "</pre>";
?>
Vea también domnode_append_child().
DomNode->insert_before
11-May-2006 05:40
