2

I'm trying to change the contents of a node in an XML file using simpleXML. I know that the variable for the new node-contents contains the right stuff, but for some reason the file isn't changed when it is saved. I'm probably missing something basic, because I'm new to simpleXML. Here is the whole php script:

<?php 
    $doc=$_REQUEST["book"];
    $div1=$_REQUEST["div1"];
    $div2=$_REQUEST["div2"];
    if ($div1=="") $div1=$_REQUEST["chapter"];
    if ($div2=="") $div2=$_REQUEST["verse"];
    $div3=$_REQUEST["div3"];
    $textresponse=$_REQUEST["xmltext"];
    $strippedresponse = "<?xml version='1.0'?>" . stripslashes($textresponse);
    echo("Saved changes to " . $doc . " " . $div1 . "." . $div2 ."<br />");    
    $fileName="/home/ocp/public_html/sites/default/docs/drafts/".$doc.".xml";
    $xmlDoc = simplexml_load_file($fileName);
    $backupFileName="/home/ocp/public_html/sites/default/docs/backups/".$doc." ".date("Y-m-d H.i.s").".xml";
    file_put_contents($backupFileName, $xmlDoc->asXML());
    $backupSize = filesize($backupFileName);
    echo("Backup {$backupFileName} created:".$backupSize." bytes<br />");
    if ($doc) {
        if ($div1) {
            if ($div2) {
                $newVerse = simplexml_load_string($strippedresponse);
        $oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); 
        $oldVerse = $newVerse;

                $newDoc = $xmlDoc->asXml();
            file_put_contents($fileName, $newDoc);
            $newSize = filesize($fileName);
                echo("New file is ".$newSize." bytes <br />");
            }
        }
    }
?>
1
  • That's a huge chunk of code, containing a lot of code unrelated to your problem. Trim it down to the bare minimum, it will make it easier for people to read it and answer. No one wants to decipher 30 lines of code for a question that can be summarized in one sentence. Commented Dec 12, 2010 at 7:59

2 Answers 2

2

I'll venture to say that this code certainly doesn't do what you want it to:

$newVerse = simplexml_load_string($strippedresponse);
$oldVerse = $xmlDoc->xpath("//div[@number='".$div1."']/div[@number='".$div2."']"); 
$oldVerse = $newVerse;

Changing the value of a PHP variable has no side-effects. In other word, nothing happens when you do $a = $b; except in some specific cases, and it's not one of them.

I don't know what you really want to achieve with this code. If you want to replace the (X)HTML inside a specific <div/> you will need to use DOM and create a DOMDocumentFragment, use appendXML() to populate it then substitute it to your old <div/>. Either that or create a new DOMDocument, loadXML() then importNode() to your old document and replaceChild() your old div.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I was clearly confused. It sounds like I need to use DOM to actually change the contents of an XML node. Back to the drawing board.
You can use either SimpleXML or DOM to change the content of a node, but you'll definitely need DOM if you want to inject XML and other DOM operations, yes.
-1

SimpleXMLElement::xpath returns an array of SimpleXMLElement objects. Copies, not references. So $oldVerse = $newVerse; does not change $xmlDoc in any way. SimpleXML is sufficient to read XML, for manipulation you might want to choose a more powerful alternative from http://www.php.net/manual/de/refs.xml.php, e.g. DOM.

2 Comments

This answer is wrong on many counts. xpath() returns an array of objects, each object "references" the node in the original document. It's not a "copy" (or clone) of the node. SimpleXML is great at reading and writing generic XML, DOM handles DOM-related operations. It's not "more powerful" it's just a different goal. Generating XML is way easier and less verbose with SimpleXML. Manipulating DOM documents is easier with DOM.
@Josh Davis: The returned values behave like references, but in fact they reference not real objects but system resources. Therefore you cannot manipulate $xmlDoc, except for changing attributes and this is rather a side effect of the exposed array interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.