No Description

XMLReader.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. /**
  20. * XML Reader wrapper
  21. *
  22. * @since 0.10.0
  23. */
  24. class XMLReader
  25. {
  26. /**
  27. * DOMDocument object
  28. *
  29. * @var \DOMDocument
  30. */
  31. private $dom = null;
  32. /**
  33. * DOMXpath object
  34. *
  35. * @var \DOMXpath
  36. */
  37. private $xpath = null;
  38. /**
  39. * Get DOMDocument from ZipArchive
  40. *
  41. * @param string $zipFile
  42. * @param string $xmlFile
  43. * @return \DOMDocument|false
  44. * @throws \PhpOffice\PhpWord\Exception\Exception
  45. */
  46. public function getDomFromZip($zipFile, $xmlFile)
  47. {
  48. if (file_exists($zipFile) === false) {
  49. throw new Exception('Cannot find archive file.');
  50. }
  51. $zip = new ZipArchive();
  52. $zip->open($zipFile);
  53. $content = $zip->getFromName($xmlFile);
  54. $zip->close();
  55. if ($content === false) {
  56. return false;
  57. } else {
  58. return $this->getDomFromString($content);
  59. }
  60. }
  61. /**
  62. * Get DOMDocument from content string
  63. *
  64. * @param string $content
  65. * @return \DOMDocument
  66. */
  67. public function getDomFromString($content)
  68. {
  69. $this->dom = new \DOMDocument();
  70. $this->dom->loadXML($content);
  71. return $this->dom;
  72. }
  73. /**
  74. * Get elements
  75. *
  76. * @param string $path
  77. * @param \DOMElement $contextNode
  78. * @return \DOMNodeList
  79. */
  80. public function getElements($path, \DOMElement $contextNode = null)
  81. {
  82. if ($this->dom === null) {
  83. return array();
  84. }
  85. if ($this->xpath === null) {
  86. $this->xpath = new \DOMXpath($this->dom);
  87. }
  88. if (is_null($contextNode)) {
  89. return $this->xpath->query($path);
  90. } else {
  91. return $this->xpath->query($path, $contextNode);
  92. }
  93. }
  94. /**
  95. * Get element
  96. *
  97. * @param string $path
  98. * @param \DOMElement $contextNode
  99. * @return \DOMElement|null
  100. */
  101. public function getElement($path, \DOMElement $contextNode = null)
  102. {
  103. $elements = $this->getElements($path, $contextNode);
  104. if ($elements->length > 0) {
  105. return $elements->item(0);
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * Get element attribute
  112. *
  113. * @param string $attribute
  114. * @param \DOMElement $contextNode
  115. * @param string $path
  116. * @return string|null
  117. */
  118. public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
  119. {
  120. $return = null;
  121. if ($path !== null) {
  122. $elements = $this->getElements($path, $contextNode);
  123. if ($elements->length > 0) {
  124. /** @var \DOMElement $node Type hint */
  125. $node = $elements->item(0);
  126. $return = $node->getAttribute($attribute);
  127. }
  128. } else {
  129. if ($contextNode !== null) {
  130. $return = $contextNode->getAttribute($attribute);
  131. }
  132. }
  133. return ($return == '') ? null : $return;
  134. }
  135. /**
  136. * Get element value
  137. *
  138. * @param string $path
  139. * @param \DOMElement $contextNode
  140. * @return string|null
  141. */
  142. public function getValue($path, \DOMElement $contextNode = null)
  143. {
  144. $elements = $this->getElements($path, $contextNode);
  145. if ($elements->length > 0) {
  146. return $elements->item(0)->nodeValue;
  147. } else {
  148. return null;
  149. }
  150. }
  151. /**
  152. * Count elements
  153. *
  154. * @param string $path
  155. * @param \DOMElement $contextNode
  156. * @return integer
  157. */
  158. public function countElements($path, \DOMElement $contextNode = null)
  159. {
  160. $elements = $this->getElements($path, $contextNode);
  161. return $elements->length;
  162. }
  163. /**
  164. * Element exists
  165. *
  166. * @param string $path
  167. * @param \DOMElement $contextNode
  168. * @return boolean
  169. */
  170. public function elementExists($path, \DOMElement $contextNode = null)
  171. {
  172. return $this->getElements($path, $contextNode)->length > 0;
  173. }
  174. }