protected function writeNode($fileHandle, ObjectId $oid, $depth) {
    $persistenceFacade = $this->getPersistenceFacade();

    // get document definition
    $docIndent = $this->getRequestValue('docIndent');
    $docLinebreak = $this->getRequestValue('docLinebreak');
    $cacheSection = $this->getRequestValue('cacheSection');

    // get document state from cache
    $lastIndent = $this->cache->get($cacheSection, self::CACHE_KEY_LAST_INDENT);
    $tagsToClose = $this->cache->get($cacheSection, self::CACHE_KEY_TAGS_TO_CLOSE);

    // load node and get element name
    $node = $persistenceFacade->load($oid);
    $elementName = $persistenceFacade->getSimpleType($node->getType());

    // check if the node is written already
    $exportedOids = $this->cache->get($cacheSection, self::CACHE_KEY_EXPORTED_OIDS);
    if (!in_array($oid->__toString(), $exportedOids)) {
      // write node
      $mapper = $node->getMapper();

      $hasUnvisitedChildren = $this->getNumUnvisitedChildren($node) > 0;

      $curIndent = $depth;
      $this->endTags($fileHandle, $curIndent);

      // write object's content
      // open start tag
      $this->fileUtil->fputsUnicode($fileHandle, str_repeat($docIndent, $curIndent).'<'.$elementName);
      // write object attributes
      $attributes = $mapper->getAttributes();
      foreach ($attributes as $curAttribute) {
        $attributeName = $curAttribute->getName();
        $value = $node->getValue($attributeName);
        $this->fileUtil->fputsUnicode($fileHandle, ' '.$attributeName.'="'.$this->formatValue($value).'"');
      }
      // close start tag
      $this->fileUtil->fputsUnicode($fileHandle, '>');
      if ($hasUnvisitedChildren) {
        $this->fileUtil->fputsUnicode($fileHandle, $docLinebreak);
      }

      // remember end tag if not closed
      if ($hasUnvisitedChildren) {
        $closeTag = ["name" => $elementName, "indent" => $curIndent];
        array_unshift($tagsToClose, $closeTag);
      }
      else {
        $this->fileUtil->fputsUnicode($fileHandle, '</'.$elementName.'>'.$docLinebreak);
      }
      // remember current indent
      $lastIndent = $curIndent;

      // register exported node
      $exportedOids[] = $oid->__toString();
      $this->cache->put($cacheSection, self::CACHE_KEY_EXPORTED_OIDS, $exportedOids);
    }

    // update document state in cache
    $this->cache->put($cacheSection, self::CACHE_KEY_LAST_INDENT, $lastIndent);
    $this->cache->put($cacheSection, self::CACHE_KEY_TAGS_TO_CLOSE, $tagsToClose);
  }