How to convert a Word document to XML using PHP?

I want to convert Word documents (.doc and .docx) to XML. How can I do this using PHP?

Once I have done this, I need to add some data to this XML file.

Can anyone help me out?

+3
source share
3 answers

Word document (docx) is an xml file . Just unzip it.

+2
source

The best way to make an XML file through PHP is with the XML DOM class.

http://www.w3schools.com/php/php_xml_dom.asp

0
source
<?php
$zip = new ZipArchive; // creating object of ZipArchive class.
$sUploadedFile = 'publisher.docx';
$zip->open("word_document/$sUploadedFile");
$aFileName = explode('.',$sUploadedFile);
$sDirectoryName =  current($aFileName);

if (!is_dir("word_document/$sDirectoryName")){
    mkdir("word_document/$sDirectoryName");
    $zip->extractTo("word_document/$sDirectoryName"); 
    copy("word_document/$sDirectoryName/word/document.xml", "xml_document/$sDirectoryName.xml");

    $xml = simplexml_load_file("xml_document/$sDirectoryName.xml");
    $xml->registerXPathNamespace('w',"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    $text = $xml->xpath('//w:t');

    echo '<pre>'; print_r($text); echo '</pre>';

    rrmdir("word_document/$sDirectoryName");
}

function rrmdir($dir) {
  if (is_dir($dir)) {
    $objects = scandir($dir);
    foreach ($objects as $object) {
      if ($object != "." && $object != "..") {
        if (filetype($dir."/".$object) == "dir") 
           rrmdir($dir."/".$object); 
        else unlink   ($dir."/".$object);
      }
    }
    reset($objects);
    rmdir($dir);
  }
 }

?>
0
source

Source: https://habr.com/ru/post/1759839/


All Articles