Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate headers when loading XML file with UTF8 headers #5

Open
GoogleCodeExporter opened this issue Mar 25, 2015 · 2 comments
Open

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 1 Oct 2013 at 10:08

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 1 Oct 2013 at 10:59

  • Added labels: ****
  • Removed labels: ****

@GoogleCodeExporter
Copy link
Author

Or there is XMLLoadFromFile helper function in OmniXMLUtils.pas

Original comment by [email protected] on 9 Dec 2014 at 3:51

  • Added labels: ****
  • Removed labels: ****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant