Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I am not sure what is different but Document.ParseOptions + [poValidateOnParse, poPreserveWhiteSpace]; was not accessible for me.

Something similar worked:

var
    xmlDoc: IXMLDOMDocument2;

xmlDoc := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument2;
xmlDoc.validateOnParse := True;
xmlDoc.preserveWhiteSpace := True;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top