/** * Private method used to read and populate anydataset class from specified file * @param string $filepath Path and Filename to be read * @return null */ private function createFrom($filepath) { if (file_exists($filepath)) { $anyDataSet = XmlUtil::createXmlDocumentFromFile($filepath); $this->_collection = array(); $rows = $anyDataSet->getElementsByTagName("row"); foreach ($rows as $row) { $sr = new SingleRow(); $fields = $row->getElementsByTagName("field"); foreach ($fields as $field) { $attr = $field->attributes->getNamedItem("name"); if (!is_null($attr)) { $sr->addField($attr->nodeValue, $field->nodeValue); } else { throw new \InvalidArgumentException('Malformed anydataset file ' . basename($filepath)); } } $sr->acceptChanges(); $this->_collection[] = $sr; } $this->_currentRow = sizeof($this->_collection) - 1; } }
/** * Add node to specific XmlNode from file existing on disk * * @param \DOMNode $rootNode XmlNode receives node * @param string $filename File to import node * @param string $nodetoadd Node to be added */ public static function addNodeFromFile(\DOMNode $rootNode, $filename, $nodetoadd) { if ($rootNode === null) { return; } if (!file_exists($filename)) { return; } try { // \DOMDocument $source = XmlUtil::createXmlDocumentFromFile($filename); $nodes = $source->getElementsByTagName($nodetoadd)->item(0)->childNodes; foreach ($nodes as $node) { $newNode = $rootNode->ownerDocument->importNode($node, true); $rootNode->appendChild($newNode); } } catch (\Exception $ex) { throw $ex; } }