open($zipFile); if ($canOpen === false) { throw new Exception('Cannot open archive file.'); } $contents = $zip->getFromName($xmlFile); $zip->close(); if ($contents === false) { return false; } else { $this->dom = new \DOMDocument(); $this->dom->loadXML($contents); return $this->dom; } } /** * Get elements * * @param string $path * @return \DOMNodeList */ public function getElements($path, \DOMNode $context = null) { if ($this->dom === null) { return array(); } if ($this->xpath === null) { $this->xpath = new \DOMXpath($this->dom); } return $this->xpath->query($path, $context); } /** * Get elements * * @param string $path * @return \DOMNodeList */ public function getElement($path, \DOMNode $context = null) { $elements = $this->getElements($path, $context); if ($elements->length > 0) { return $elements->item(0); } else { return false; } } /** * Get element attribute * * @param string|\DOMNode $path * @param string $attribute * @return null|string */ public function getAttribute($path, $attribute, \DOMNode $context = null) { if ($path instanceof \DOMNode) { $return = $path->getAttribute($attribute); } else { $elements = $this->getElements($path, $context); if ($elements->length > 0) { $return = $elements->item(0)->getAttribute($attribute); } else { $return = ''; } } return ($return == '') ? null : $return; } /** * Get element value * * @param string $path * @return null|string */ public function getValue($path, \DOMNode $context = null) { $elements = $this->getElements($path, $context); if ($elements->length > 0) { $return = $elements->item(0)->nodeValue; } else { $return = ''; } return ($return == '') ? null : $return; } /** * Count elements * * @param string $path * @return \DOMNodeList */ public function countElements($path, \DOMNode $context = null) { $elements = $this->getElements($path, $context); return $elements->length; } /** * Element exists * * @param string $path * @return \DOMNodeList */ public function elementExists($path, \DOMNode $context = null) { return $this->getElements($path, $context)->length > 0; } }