otrdiena, 2012. gada 18. septembris

Delphi, xml and parse/format doc with two space ident

Today I got to solve xml format problem with Delphi. It appears that by default delphi uses tab for xml element indent. I had to find some hacks to work it properly and I think it's worth to share.

So before processing I did:


    Doc := TXMLDocument.Create(nil);
    Doc.ParseOptions := Doc.ParseOptions + [poPreserveWhiteSpace];

That seems to remove default behaviour of indenting with tabs. But now when I insert new node I get it without indents at all. So I have to "reparse" doc before save:

    Doc.XML.Text := xmlDoc.FormatXMLData(Doc.XML.Text);
    Doc.Active := true;

I did not experiment with - Doc.NodeIndentStr := '  '; because two spaces is used by default.

Happy hacking!

Edit:
I found that FormatXMLData loose my utf-8 encoding. So I had to do:


    Encoding := Doc.Encoding;
    Doc.XML.Text := xmlDoc.FormatXMLData(Doc.XML.Text);
    Doc.Active := true;
    Doc.Encoding := Encoding;


Nav komentāru:

Ierakstīt komentāru