1818namespace PhpOffice \Common ;
1919
2020use DOMDocument ;
21- use DOMElement ;
22- use DOMNodeList ;
2321use DOMXpath ;
2422use ZipArchive ;
2523
@@ -33,24 +31,24 @@ class XMLReader
3331 /**
3432 * DOMDocument object
3533 *
36- * @var DOMDocument
34+ * @var \ DOMDocument
3735 */
38- private $ dom = null ;
36+ private $ dom ;
3937
4038 /**
4139 * DOMXpath object
4240 *
43- * @var DOMXpath
41+ * @var \ DOMXpath
4442 */
45- private $ xpath = null ;
43+ private $ xpath ;
4644
4745 /**
4846 * Get DOMDocument from ZipArchive
4947 *
5048 * @param string $zipFile
5149 * @param string $xmlFile
5250 *
53- * @return DOMDocument|false
51+ * @return \ DOMDocument|false
5452 *
5553 * @throws \Exception
5654 */
@@ -60,9 +58,15 @@ public function getDomFromZip(string $zipFile, string $xmlFile)
6058 throw new \Exception ('Cannot find archive file. ' );
6159 }
6260
63- $ zip = new ZipArchive ();
61+ $ zip = new \ ZipArchive ();
6462 $ zip ->open ($ zipFile );
6563 $ content = $ zip ->getFromName ($ xmlFile );
64+
65+ // Files downloaded from Sharepoint are somehow different and fail on the leading slash.
66+ if ($ content === false && substr ($ xmlFile , 0 , 1 ) === '/ ' ) {
67+ $ content = $ zip ->getFromName (substr ($ xmlFile , 1 ));
68+ }
69+
6670 $ zip ->close ();
6771
6872 if ($ content === false ) {
@@ -77,7 +81,7 @@ public function getDomFromZip(string $zipFile, string $xmlFile)
7781 *
7882 * @param string $content
7983 *
80- * @return DOMDocument
84+ * @return \ DOMDocument
8185 */
8286 public function getDomFromString (string $ content )
8387 {
@@ -86,7 +90,7 @@ public function getDomFromString(string $content)
8690 $ originalLibXMLEntityValue = libxml_disable_entity_loader (true );
8791 }
8892
89- $ this ->dom = new DOMDocument ();
93+ $ this ->dom = new \ DOMDocument ();
9094 $ this ->dom ->loadXML ($ content );
9195
9296 if (\PHP_VERSION_ID < 80000 ) {
@@ -100,17 +104,17 @@ public function getDomFromString(string $content)
100104 * Get elements
101105 *
102106 * @param string $path
103- * @param DOMElement $contextNode
107+ * @param \ DOMElement $contextNode
104108 *
105- * @return DOMNodeList<DOMElement>
109+ * @return \ DOMNodeList<\ DOMElement>
106110 */
107- public function getElements (string $ path , DOMElement $ contextNode = null )
111+ public function getElements (string $ path , \ DOMElement $ contextNode = null )
108112 {
109113 if ($ this ->dom === null ) {
110- return new DOMNodeList ();
114+ return new \ DOMNodeList ();
111115 }
112116 if ($ this ->xpath === null ) {
113- $ this ->xpath = new DOMXpath ($ this ->dom );
117+ $ this ->xpath = new \ DOMXpath ($ this ->dom );
114118 }
115119
116120 if (is_null ($ contextNode )) {
@@ -136,7 +140,7 @@ public function registerNamespace($prefix, $namespaceURI)
136140 throw new \InvalidArgumentException ('Dom needs to be loaded before registering a namespace ' );
137141 }
138142 if ($ this ->xpath === null ) {
139- $ this ->xpath = new DOMXpath ($ this ->dom );
143+ $ this ->xpath = new \ DOMXpath ($ this ->dom );
140144 }
141145
142146 return $ this ->xpath ->registerNamespace ($ prefix , $ namespaceURI );
@@ -146,15 +150,15 @@ public function registerNamespace($prefix, $namespaceURI)
146150 * Get element
147151 *
148152 * @param string $path
149- * @param DOMElement $contextNode
153+ * @param \ DOMElement $contextNode
150154 *
151- * @return DOMElement|null
155+ * @return \ DOMElement|null
152156 */
153- public function getElement ($ path , DOMElement $ contextNode = null ): ?DOMElement
157+ public function getElement ($ path , \ DOMElement $ contextNode = null ): ?\ DOMElement
154158 {
155159 $ elements = $ this ->getElements ($ path , $ contextNode );
156160 if ($ elements ->length > 0 ) {
157- return $ elements ->item (0 ) instanceof DOMElement ? $ elements ->item (0 ) : null ;
161+ return $ elements ->item (0 ) instanceof \ DOMElement ? $ elements ->item (0 ) : null ;
158162 }
159163
160164 return null ;
@@ -164,18 +168,18 @@ public function getElement($path, DOMElement $contextNode = null): ?DOMElement
164168 * Get element attribute
165169 *
166170 * @param string $attribute
167- * @param DOMElement $contextNode
171+ * @param \ DOMElement $contextNode
168172 * @param string $path
169173 *
170174 * @return string|null
171175 */
172- public function getAttribute ($ attribute , DOMElement $ contextNode = null , $ path = null )
176+ public function getAttribute ($ attribute , \ DOMElement $ contextNode = null , $ path = null )
173177 {
174178 $ return = null ;
175179 if ($ path !== null ) {
176180 $ elements = $ this ->getElements ($ path , $ contextNode );
177181 if ($ elements ->length > 0 ) {
178- /** @var DOMElement $node Type hint */
182+ /** @var \ DOMElement $node Type hint */
179183 $ node = $ elements ->item (0 );
180184 $ return = $ node ->getAttribute ($ attribute );
181185 }
@@ -192,11 +196,11 @@ public function getAttribute($attribute, DOMElement $contextNode = null, $path =
192196 * Get element value
193197 *
194198 * @param string $path
195- * @param DOMElement $contextNode
199+ * @param \ DOMElement $contextNode
196200 *
197201 * @return string|null
198202 */
199- public function getValue ($ path , DOMElement $ contextNode = null )
203+ public function getValue ($ path , \ DOMElement $ contextNode = null )
200204 {
201205 $ elements = $ this ->getElements ($ path , $ contextNode );
202206 if ($ elements ->length > 0 ) {
@@ -210,11 +214,11 @@ public function getValue($path, DOMElement $contextNode = null)
210214 * Count elements
211215 *
212216 * @param string $path
213- * @param DOMElement $contextNode
217+ * @param \ DOMElement $contextNode
214218 *
215219 * @return int
216220 */
217- public function countElements ($ path , DOMElement $ contextNode = null )
221+ public function countElements ($ path , \ DOMElement $ contextNode = null )
218222 {
219223 $ elements = $ this ->getElements ($ path , $ contextNode );
220224
@@ -225,11 +229,11 @@ public function countElements($path, DOMElement $contextNode = null)
225229 * Element exists
226230 *
227231 * @param string $path
228- * @param DOMElement $contextNode
232+ * @param \ DOMElement $contextNode
229233 *
230234 * @return bool
231235 */
232- public function elementExists ($ path , DOMElement $ contextNode = null )
236+ public function elementExists ($ path , \ DOMElement $ contextNode = null )
233237 {
234238 return $ this ->getElements ($ path , $ contextNode )->length > 0 ;
235239 }
0 commit comments