kroimon/omnixml

Duplicate headers when loading XML file with UTF8 headers

Opened this issue · 2 comments

What steps will reproduce the problem?
1. Load sample XML file with <?xml ... ?> header (see below)
2. Save file back to disk

What is the expected output? What do you see instead?
I expected to see a single header line, but after loading the document from 
disk an additional header line is added (and subsequently written back to disk).

What version of the product are you using? On what operating system?
v1.16 (2013-07-01) on Delphi XE2 / Win 7 x64

Please provide any additional information below.

Sample file:
data.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<system>
  <items>
    <item/>
  </items>
</system>

var
  gDoc :  IXMLDocument;
begin
  gDoc := CreateXMLDoc;
  gDoc.Load('data.xml');

gDoc.XML now contains:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<system>
  <items>
    <item/>
  </items>
</system>

Original issue reported on code.google.com by bduncanj@gmail.com on 1 Oct 2013 at 10:08

It looks like what was happening is that after parsing the first line of the 
file (OmniXml.pas line 3411) the code tries to detect the encoding of the file, 
and if found, rewinds the input stream reader so that it can read the contents 
a second time.

I'm guessing that the output pointer isn't reset, and so on the second pass 
(now with the correct encoding) it re-reads the first line into the output file 
(or appends that same element to the XML structure).

I worked around this by using the following code to load my XML file:

Doc := CreateXMLDoc;
  FS := TFileStream.Create('data.xml', fmOpenRead or fmShareDenyNone);
  try
    Doc.LoadFromStream(FS, CP_UTF8);
  finally
    FS.Free;
  end;

Original comment by bduncanj@gmail.com on 1 Oct 2013 at 10:59

Or there is XMLLoadFromFile helper function in OmniXMLUtils.pas

Original comment by Arioch...@gmail.com on 9 Dec 2014 at 3:51