First I tried doing it with simpleXML, something like this:
Code: Select all
<?php
function replaceNode($xml, $query, $value){
$queryResult = $xml->xpath($query);
if ($queryResult){
echo 'node <i>' . $query . '</i> exists, replacing value ';
foreach ($queryResult as $node){
// replacing the value
$node =$value;
}
}
else {
// TODO: if query doesn't return node, create new node
}
return $xml;
}
?>Then I tried to use the DOM but I got stuck trying to replace the value of the node. I can append the new value to the old value, but I can't figure out how to replace it.
I'm thinking something like DOMCharacterData->replaceData() is the answer here but I'm not sure what DomCharacterData is or how to access it.
I'm kind of new to this and I'm trying to learn more about working with the DOM and simpleXML so it would greatly apreciated if anyone could point me in the right direction. Links to tutorials about using the DOM/simpleXML with PHP 5 would also be great.
Thanks,
Jeroen