title: SAXParseException - The element must be terminated by the matching end tag (The XML file is valid)
body: <p>I am am getting a strange error in my DocumentBuilderFactory parser, the error is the following:</p>
  <pre><code>[Fatal Error] standard_000000_3.xml:1221888:48: The element type &quot;tduid&quot; must be terminated by the matching end-tag &quot;&lt;/tduid&gt;&quot;.
  org.xml.sax.SAXParseException; systemId: file:/home/000000/new/standard_000000_3.xml; lineNumber: 1221888; columnNumber: 48; The element type &quot;tduid&quot; must be terminated by the matching end-tag &quot;&lt;/tduid&gt;&quot;.
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
      at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
      at com.company.batch.BatchReader.&lt;init&gt;(BatchReader.java:46)
      at com.company.batch.BatchFile.open(BatchFile.java:76)
      at application.Daemon.checkNewImportFiles(Daemon.java:385)
      at application.Daemon.startApplication(Daemon.java:68)
      at application.Daemon.run(Daemon.java:36)
  [Fatal Error] standard_000000_9.xml:1049516:32: XML document structures must start and end within the same entity.
  org.xml.sax.SAXParseException; systemId: file:/home/000000/new/standard_XXXXXX_9.xml; lineNumber: 1049516; columnNumber: 32; XML document structures must start and end within the same entity.
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
      at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
      at com.company.batch.BatchReader.&lt;init&gt;(BatchReader.java:46)
      at com.company.batch.BatchFile.open(BatchFile.java:76)
      at application.Daemon.checkNewImportFiles(Daemon.java:385)
      at application.Daemon.startApplication(Daemon.java:68)
      at application.Daemon.run(Daemon.java:36)
  </code></pre>
  <p>I know that these are usually errors of the structure of the xml, but is not this the case, I tried to validate it with xmllist and strange thing, if I create a java application with only the class that is managing the file and inserting in the main only the call to open it, change 2-3 attributes and save it, it is working, and the error is not happening.</p>
  <p>I thought it may be a problem of memory so I tried to run the process monitoring the amount of memory used, now on the system the maximum java memory is 900mb and the program is not requiring more than 400.</p>
  <p>An example of the xml file at the point in which is happening the error (first error, the error is happening on the starting tag <strong>&lt;onPurchase&gt;</strong>):</p>
  <pre><code>&lt;transaction&gt;
    &lt;eventId&gt;123456&lt;/eventId&gt;
    &lt;orderNumber&gt;TEST_ORDER&lt;/orderNumber&gt;
    &lt;orderValue&gt;0&lt;/orderValue&gt;
    &lt;currency&gt;USD&lt;/currency&gt;
    &lt;tduid&gt;testtesttesttesttesttesttesttest&lt;/tduid&gt;
    &lt;timestamp&gt;2016-03-05 15:23:00 GMT&lt;/timestamp&gt;
    &lt;extraReportingInfo&gt;
      &lt;isUniveralStoreNewPurchaser&gt;True&lt;/isUniveralStoreNewPurchaser&gt;
      &lt;onEntry&gt;
        &lt;productType&gt;Test product&lt;/productType&gt;
        &lt;tuner&gt;TEST TUNER&lt;/tuner&gt;
        &lt;userOs&gt;Linux[2.0.10340.184]&lt;/userOs&gt;
        &lt;userDevice&gt;Linux.Ubuntu&lt;/userDevice&gt;
      &lt;/onEntry&gt;
      &lt;onPurchase&gt;
        &lt;productType&gt;Test Product&lt;/productType&gt;
        &lt;tuner&gt;TEST TUNER&lt;/tuner&gt;
        &lt;contentOwnership&gt;TST&lt;/contentOwnership&gt;
        &lt;userDevice&gt;Unknown&lt;/userDevice&gt;
      &lt;/onPurchase&gt;
    &lt;/extraReportingInfo&gt;
    &lt;reportInfo&gt;
      &lt;item&gt;
        &lt;productNumber&gt;PRODUCT_NUMBER&lt;/productNumber&gt;
        &lt;productName&gt;PRODUCT_NAME&lt;/productName&gt;
        &lt;price&gt;0&lt;/price&gt;
        &lt;quantity&gt;1&lt;/quantity&gt;
      &lt;/item&gt;
    &lt;/reportInfo&gt;
  &lt;/transaction&gt;
  </code></pre>
  <p>Following the code that is managing to load the xml file:</p>
  <pre><code>public BatchReader(String Filename) {
      try {
      this.filename = Filename;
      File XMLFile = new File(Filename);
      DocumentBuilderFactory DBFactory = DocumentBuilderFactory.newInstance();
  
      DocumentBuilder DBBuilder = DBFactory.newDocumentBuilder();
      DBFactory.setValidating(true);
      this.doc = DBBuilder.parse(XMLFile);
      
      // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
      this.doc.getDocumentElement().normalize();
      
      // Added for debug
      System.out.println(XMLFile.getAbsolutePath());
  
      // Setting the batch type
      this.Type = &quot;standard&quot;;
  
      
      this.organizationId = Integer.parseInt(this.getString(&quot;organizationId&quot;, this.doc.getDocumentElement()));
  
      
      this.Sequence = (this.getString(&quot;sequenceNumber&quot;, this.doc.getDocumentElement()) != null) ? Integer.parseInt(this.getString(&quot;sequenceNumber&quot;, this.doc.getDocumentElement())) : 0;
      this.checksum = (this.getString(&quot;checksum&quot;, this.doc.getDocumentElement()) != null) ? this.getString(&quot;checksum&quot;, this.doc.getDocumentElement()) : null;
      
      //this.checksum = this.getString(&quot;checksum&quot;, this.doc.getDocumentElement());
  
      
      this.txAmount = this.doc.getElementsByTagName(&quot;transaction&quot;).getLength();
  
      } catch ( NullPointerException | NumberFormatException e) {
          e.printStackTrace();
      } catch (ParserConfigurationException | SAXException | IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>The error happens on this line:</p>
  <pre><code>this.doc = DBBuilder.parse(XMLFile);
  </code></pre>
  <p>The file is 73 Mb.<br />
  I sincerely don't know where else looking for the problem,<br />
  could you help me please?</p>
  <hr />
  <p><em>Edit:</em></p>
  <p>In a file I think I found the error, in a line:
  <code>&lt;productName&gt;ÐŸÑÑ‚Ð½Ð¸Ñ†Ð°!&lt;/productName&gt;</code>.</p>
  <p>This seems to have been codified in ansi, instead of UTF-8.</p>
  <p>I'm gonna check in the other files, if I find something similar.</p>