Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IXMLDocument.SaveToFile() uses tab character for indentation instead of spaces

I have an XML file, which is originally formatted using space indents (2 spaces for each nested item).

When I load and save this file using IXMLDocument, space indents are changing to the tab characters (code #9).

Here is the code:

 var
   FileName: String;
   Document: IXMLDocument;
 ...
 Document := XMLDoc.LoadXMLDocument(FileName);
 Document.SaveToFile(FileName);

I tried to use NodeIndentStr property - no result:

 Document := XMLDoc.LoadXMLDocument(FileName);
 Document.NodeIndentStr := '  ';
 Document.SaveToFile(FileName);

Used FormatXMLData too - no result:

 Document := XMLDoc.LoadXMLDocument(FileName);
 Document.XML.Text := XMLDoc.FormatXMLData(Document.XML.Text);
 Document.Active := True;
 Document.SaveToFile(FileName);

How can I save with space indents instead of tab characters?

like image 693
Andrew Avatar asked Apr 18 '12 07:04

Andrew


People also ask

Should XML have tabs or spaces?

No, the XML spec define whitespace like this: In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document.

Can you use tabs in XML?

You cannot have spaces and tabs in the tag (i.e., name) of an XML elements, see the specs: http://www.w3.org/TR/REC-xml/#NT-STag. Beside alphanumeric characters, colon, underscore, dash and dot characters are allowed in a name, and the first letter cannot be a dash or a dot.


1 Answers

There is an option in IXMLDocument where the parser can be told to preserve white spaces.

Use it like this :

Document.ParseOptions := 
  Document.ParseOptions+[poValidateOnParse]+[poPreserveWhiteSpace]; 

Disclaimer: I haven't tried it.

like image 110
LU RD Avatar answered Sep 20 '22 04:09

LU RD