2014-03-12 00:12:55 +07:00
|
|
|
<?php
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2018-03-08 23:52:25 +01:00
|
|
|
* @copyright 2010-2018 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-03-30 14:15:23 +07:00
|
|
|
*/
|
|
|
|
|
|
2015-11-15 13:33:05 +04:00
|
|
|
namespace PhpOffice\PhpWord;
|
2014-03-12 00:12:55 +07:00
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* DOM wrapper class
|
|
|
|
|
*/
|
2014-03-12 00:12:55 +07:00
|
|
|
class XmlDocument
|
|
|
|
|
{
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* Path
|
|
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @var string
|
2014-03-30 14:15:23 +07:00
|
|
|
*/
|
2014-03-12 00:12:55 +07:00
|
|
|
private $path;
|
|
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* DOMDocument object
|
|
|
|
|
*
|
|
|
|
|
* @var \DOMDocument
|
|
|
|
|
*/
|
2014-03-12 00:12:55 +07:00
|
|
|
private $dom;
|
|
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
2018-07-14 02:54:17 +02:00
|
|
|
* DOMXPath object
|
2014-03-30 14:15:23 +07:00
|
|
|
*
|
2018-07-14 02:54:17 +02:00
|
|
|
* @var \DOMXPath
|
2014-03-30 14:15:23 +07:00
|
|
|
*/
|
2014-03-12 00:12:55 +07:00
|
|
|
private $xpath;
|
|
|
|
|
|
2014-03-30 14:15:23 +07:00
|
|
|
/**
|
|
|
|
|
* File name
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2014-03-12 00:12:55 +07:00
|
|
|
private $file;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Create new instance
|
|
|
|
|
*
|
2014-03-12 00:12:55 +07:00
|
|
|
* @param string $path
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($path)
|
|
|
|
|
{
|
|
|
|
|
$this->path = realpath($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get DOM from file
|
|
|
|
|
*
|
2014-03-12 00:12:55 +07:00
|
|
|
* @param string $file
|
|
|
|
|
* @return \DOMDocument
|
|
|
|
|
*/
|
|
|
|
|
public function getFileDom($file = 'word/document.xml')
|
|
|
|
|
{
|
|
|
|
|
if (null !== $this->dom && $file === $this->file) {
|
|
|
|
|
return $this->dom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->xpath = null;
|
2014-03-12 13:30:02 +07:00
|
|
|
$this->file = $file;
|
2014-03-12 00:12:55 +07:00
|
|
|
|
2014-03-12 13:30:02 +07:00
|
|
|
$file = $this->path . '/' . $file;
|
2018-07-14 00:50:01 +02:00
|
|
|
libxml_disable_entity_loader(false);
|
2014-03-23 10:32:08 +04:00
|
|
|
$this->dom = new \DOMDocument();
|
2014-03-12 00:12:55 +07:00
|
|
|
$this->dom->load($file);
|
2018-07-14 00:50:01 +02:00
|
|
|
libxml_disable_entity_loader(true);
|
2017-11-04 22:44:12 +01:00
|
|
|
|
2014-03-12 00:12:55 +07:00
|
|
|
return $this->dom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get node list
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return \DOMNodeList
|
2014-03-12 00:12:55 +07:00
|
|
|
*/
|
2014-03-13 19:09:35 +07:00
|
|
|
public function getNodeList($path, $file = 'word/document.xml')
|
2014-03-12 00:12:55 +07:00
|
|
|
{
|
2015-02-06 22:28:31 +04:00
|
|
|
if (null === $this->dom || $file !== $this->file) {
|
2014-03-12 00:12:55 +07:00
|
|
|
$this->getFileDom($file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (null === $this->xpath) {
|
2018-07-14 02:54:17 +02:00
|
|
|
$this->xpath = new \DOMXPath($this->dom);
|
2017-12-13 07:47:49 +01:00
|
|
|
$this->xpath->registerNamespace('w14', 'http://schemas.microsoft.com/office/word/2010/wordml');
|
2014-03-12 00:12:55 +07:00
|
|
|
}
|
|
|
|
|
|
2014-03-13 19:09:35 +07:00
|
|
|
return $this->xpath->query($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get element
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return \DOMElement
|
2014-03-13 19:09:35 +07:00
|
|
|
*/
|
|
|
|
|
public function getElement($path, $file = 'word/document.xml')
|
|
|
|
|
{
|
|
|
|
|
$elements = $this->getNodeList($path, $file);
|
|
|
|
|
|
2014-03-12 00:12:55 +07:00
|
|
|
return $elements->item(0);
|
|
|
|
|
}
|
2014-03-12 10:09:42 -04:00
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get file name
|
|
|
|
|
*
|
2014-03-12 10:09:42 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getFile()
|
|
|
|
|
{
|
|
|
|
|
return $this->file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get path
|
|
|
|
|
*
|
2014-03-12 10:09:42 -04:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getPath()
|
|
|
|
|
{
|
|
|
|
|
return $this->path;
|
|
|
|
|
}
|
2014-03-13 19:09:35 +07:00
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Get element attribute
|
|
|
|
|
*
|
2014-03-13 19:09:35 +07:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $attribute
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getElementAttribute($path, $attribute, $file = 'word/document.xml')
|
|
|
|
|
{
|
|
|
|
|
return $this->getElement($path, $file)->getAttribute($attribute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-30 14:15:23 +07:00
|
|
|
* Check if element exists
|
|
|
|
|
*
|
2014-03-13 19:09:35 +07:00
|
|
|
* @param string $path
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function elementExists($path, $file = 'word/document.xml')
|
|
|
|
|
{
|
|
|
|
|
$nodeList = $this->getNodeList($path, $file);
|
2017-11-04 22:44:12 +01:00
|
|
|
|
2014-03-13 19:09:35 +07:00
|
|
|
return !($nodeList->length == 0);
|
|
|
|
|
}
|
2017-11-06 21:47:02 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the xml, or part of it as a formatted string
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-11-09 00:41:56 +01:00
|
|
|
public function printXml($path = '/', $file = 'word/document.xml')
|
2017-11-06 21:47:02 +01:00
|
|
|
{
|
2017-11-09 00:41:56 +01:00
|
|
|
$element = $this->getElement($path, $file);
|
|
|
|
|
if ($element instanceof \DOMDocument) {
|
|
|
|
|
$element->formatOutput = true;
|
|
|
|
|
$element->preserveWhiteSpace = false;
|
|
|
|
|
|
|
|
|
|
return $element->saveXML();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 21:47:02 +01:00
|
|
|
$newdoc = new \DOMDocument();
|
|
|
|
|
$newdoc->formatOutput = true;
|
|
|
|
|
$newdoc->preserveWhiteSpace = false;
|
2017-11-09 00:41:56 +01:00
|
|
|
$node = $newdoc->importNode($element, true);
|
2017-11-06 21:47:02 +01:00
|
|
|
$newdoc->appendChild($node);
|
|
|
|
|
|
|
|
|
|
return $newdoc->saveXML($node);
|
|
|
|
|
}
|
2014-03-24 00:26:10 +07:00
|
|
|
}
|