Search code examples
phpxmlquerypath

When I modify docx xml file [document.xml] with PHP QueryPath 2.1.2 changes are not visibile in actual test1.docx file, why?


I am trying to learn how to modify .docx and .odt files with PHP [QueryPath] and when I run this script [below] from cmd.exe [command line] on WAMP, on the command line it shows that changes that I want have been done and all text inside tags have been changed, but when I open an actual docx file or its xml equivalent - none of the changes have been made. Why? And how can I rectify this? Thanks in advance.

    <?php

require 'src/QueryPath/QueryPath.php';

$file = 'zip://test1.docx#word/document.xml';

$doc = qp($file);

foreach($doc->find('w|t') as $item) {
        $item->text('BLABLABLA')->writeXML();
}


?>

Solution

  • From the documentation for writeXML:

    Write the document to a file path, if given, or to stdout (usually the client).

    Since you're not passing anything to writeXML it's going to just display the modified XML, not save it to the file.

    Also, you should modify the code to only call writeXML once, at the end of your script, i.e.

    $doc->writeXML($filename);