title: Read XML files in R crashes in loops
body: <p>I have a code that loops over a list of xml files and process them. When I execute the code for a single xml, the result is the expected one. But when I start the loop there are unexpected errors in different files. The error's file changes in every iteration so there is no pattern in order to find the error.</p>
  
  <p>Ex. single file:</p>
  
  <pre><code>p_xml &lt;- function(file)
  {
     tmp&lt;-tryCatch(
    {
      (read_xml(path_XML))
    },error=function(e)
    {
      return(NA)
     })
  
   if(is.na(tmp))
     {
      file &lt;- read_xml(path_XML, encoding = "ISO-8859-1")
    }else{
     file &lt;- tmp
   }
   id &lt;- as.numeric(xml_attr(file, "id"))
   year_id &lt;- as.numeric(xml_attr(file, "machine_year"))
  
  ....
  
  return(data)
  
  }
  </code></pre>
  
  <p>This code returns a data.table in the correct way. But if I execute this function inside a loop:</p>
  
  <pre><code>global_dt&lt;-data.table()
  for(j in 1:length(file_names))
    {
      current_file &lt;- file_names[j]
      f &lt;- p_xml(file.path(current_dir,current_file))
      global_dt&lt;-rbind(global_dt,f)
    }
  </code></pre>
  
  <p>I get an error like this one:</p>
  
  <blockquote>
    <ul>
    <li>Error in doc_parse_file(con, encoding = encoding, as_html = as_html, options = options) :    Failed to parse /path/file.xml
    *</li>
    </ul>
  </blockquote>
  
  <p>The fact is that if I then execute the single code with the failed file, it returns what I expected.
  I use the xml2 library in order to read the files</p>