Пример #1
0
 /**
  * Parse an xml file and saves it to the db keeping the original
  * tree structure
  * 
  * @param string $xmlFile, xml file name
  * @return bool
  */
 public function saveDocument($xmlFile)
 {
     $this->_xmlDocument->setName(basename($xmlFile));
     if (!$this->_reader->open($xmlFile)) {
         throw new \Exception('Invalid XML');
     }
     while ($this->_reader->read()) {
         /**
          * Looks for the document's root element
          */
         if ($this->_reader->nodeType == \XMLReader::ELEMENT) {
             $xmlTag = new XmlTag();
             $xmlTag->setTagName($this->_reader->name);
             $xmlTag->setTagId($this->_tagCounter);
             /**
              * We're only storing the first attribute if present
              */
             if ($this->_reader->moveToFirstAttribute()) {
                 $attr = new XmlAttribute();
                 $attr->setTagId($xmlTag->getTagId());
                 $attr->setName($this->_reader->name);
                 $this->_reader->next();
                 $attr->setValue($this->_reader->value);
                 $xmlTag->addAttribute($attr);
             }
             $this->_xmlDocument->addTag($xmlTag);
             $this->_tagCounter++;
             $this->_parse($xmlTag);
         }
     }
     if (!$this->_xmlDocument->hasTags()) {
         throw new \Exception('The document is empty');
     }
     return $this->_repository->saveDocument($this->_xmlDocument);
 }