PHP : some comments about the dom->replaceChild() method

 2006-02-18

1) If your XPath query returns a NodeList including a unique item, or if you know for sure the order of the items returned, you can use the "item(n)" syntax instead of the "foreach" syntax.
This can greatly improve you code lisibility.

....................................................

2) Also, as the method name implies, replaceChild cannot replace a node itself but a child of a node.
Still it is possible to replace a node pointed by XPath istead of its child.
The trick is to use replaceChild on the parent node of your Xpath query result.

<?xml version="1.0" ?>
<action>
<FR>French Text</FR>
</action>

<?php
$frag = $doc->createElement("EN");
$fragA = $doc->createTextNode("English Text");
$frag->appendChild($fragA);

$xpResult = $xp->query("/action/FR");
$blipblip = $xpResult->item(0)->parentNode->replaceChild($fragA, $xpResult->item(0));
?>

Et voilà !

This produces :
<?xml version="1.0" ?>
<action>
<EN<English Text</EN>
</action>

....................................................

3) Also, be carefull, you CANNOT replace a node that doesn't exist.
While this may seems obvious, it is easy to forget.

Consider this :
<?xml version="1.0" ?>
<action>
<EN></EN>
</action>

You cannot use replaceChild() to turn this into :
<?xml version="1.0" ?>
<action>
<EN>Some text</EN>
</action>

The reason is that since the <EN></EN> element is empty, it has no child (this is clearer to understand if you consider that <EN></EN> can be written <EN />).
The fact that you intend to put some text inbetween <EN> and </EN> does not change the fact that it has no text yet, thus no child yet.
When dealing with DOM, do not take your dream for the reality. The DOM parser doen't care about your dreams. If an element is currently empty, it has no child, whatever you intend to fill in.

Thus, the solution to teh problem is to use appendChild intead of replaceChild :

<?php
$fragA = $doc->createTextNode("Some Text");
$xpResult = $xp->query("/action/EN");
$blipblip = $xpResult->item(0)->appendChild($fragA);
?>

This produces the awaited :
<?xml version="1.0" ?>
<action>
<EN>Some Text</EN>
</action>

....................................................

4) Note that the description of replaceChild in the doc is wrong. Arguments have been inverted.
The correct description is :
object DOMNode->replaceChild (object newnode, object oldnode)

Préparer cette page en vue de l'impression Envoyer cette page par e-mail

  Voir et ajouter des commentaires

Nouveautés sous cette rubrique :
PHP : array_shift  (English follows)La fonction PHP array_shift peut être assez gourmande en ressou (...)
PHP : transformToDoc vs. transformToXML ?  English translation follows.Dans la plupart des cas, si vous attendez une sortie (...)
Hotmail et UTF-8  UTF-8 est la lingua franca du web : cette méthode d'encodage permet d'utiliser s (...)
Support de l'UTF-8 dans divers clients MySQL  De nombreux clients (GUI) pour base de donnée MySQL prétendent un "support compl (...)
Comment to the DOMNamedNodeMap ->getNamedItem() page in the php.net manual  Basic example of use :< ? php(...)echo $doc->documentElement->attributes->getNam (...)

Easter Flakes • Flocons de Pâques | Nous contacter
© Easter Flakes - 2007. Tous droits réservés sur toutes images, textes et codes. Valid XHTML 1.0 Transitional

Server monitoring / website monitoring with Livewatch.de
Server Monitoring with Livewatch.de