EldoS | Feel safer!

Software components for data protection, secure storage and transfer

XMLDocument - normalizing lines

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
#17758
Posted: 10/06/2011 09:07:51
by Gianni Di Pietrantonio (Standard support level)
Joined: 07/27/2011
Posts: 7

Is it possible to save XMLDocument normalizing end of lines with cr and lf during SaveToStream operation?

Best Regards
#17760
Posted: 10/06/2011 09:33:14
by Dmytro Bogatskyy (EldoS Corp.)

It depends on what you mean by "normalizing end of lines with cr and lf during SaveToStream operation".
The normalization option that is set on LoadFromStream method and is on by default, instructs a parser to replace CRLF sequences and CR characters with LF characters when an xml document is loaded. So, in this case, it is not possible to restore CR characters.
If you disable this normalization option in LoadFromStream method, then CR characters will be saved too.
#17762
Posted: 10/06/2011 10:15:58
by Gianni Di Pietrantonio (Standard support level)
Joined: 07/27/2011
Posts: 7

I'm not opening an existing file. I'am creating new xml document.

FXMLDocument := TElXMLDOMDocument.Create;

Then I append nodes with
NodeRoot:=FXMLDocument.AppendChild(FXMLDocument.CreateElement('doc'));

.....
NodeChild:=FXMLDocument.CreateElement('dlean');
NodeRoot.AppendChild(NodeChild);
NodeChild:=FXMLDocument.CreateElement('dirty');
NodeRoot.AppendChild(NodeChild);
....
then i save
FXMLDocument.SaveToStream(F, xcmNone);
...
In this way no cr&lf are generated between elements.
<doc> <clean></clean> <dirty></dirty> </doc>

I like to have an xml file in this form.

<doc>
<clean></clean>
<dirty></dirty>
</doc>
#17768
Posted: 10/06/2011 12:38:50
by Dmytro Bogatskyy (EldoS Corp.)

There is no such function to reformat an xml document.
But it could be easily done with a similar function:
Code
procedure FormatElement(ParentNode : TElXMLDOMElement; Level : Integer);
var
  Node : TElXMLDOMNode;
  s : XMLString;
  i: Integer;
begin
  s := #10;
  for i := 0 to Level do
    s := s + #9;

  Node := ParentNode.FirstChild;
  while Assigned(Node) do
  begin
    if Node.NodeType = ntElement then
    begin
      SBXMLUtils.InsertTextBeforeXMLElement(s, ParentNode, Node);  

      FormatElement(TElXMLDOMElement(Node), Level + 1);

      AddTextToXMLElement(s, TElXMLDOMElement(Node));
    end;
    
    Node := Node.NextSibling;
  end;
end;

FormatElement(FXMLDocument.DocumentElement, 0);
#17769
Posted: 10/06/2011 13:55:43
by Gianni Di Pietrantonio (Standard support level)
Joined: 07/27/2011
Posts: 7

This formatting is not good to read, but acceptable.
#24507
Posted: 04/09/2013 08:27:30
by Paolo Righi (Standard support level)
Joined: 02/15/2008
Posts: 34

Hi,

the following version of FormatElement gives better results:
Code
    procedure FormatElement(ParentNode : TElXMLDOMElement; Level : Integer);
    var
      Node : TElXMLDOMNode;
      s : XMLString;
      i: Integer;
    begin
      s := #10;
      for i := 0 to Level do
        s := s + #9;
    
      Node := ParentNode.FirstChild;
      while Assigned(Node) do
      begin
        if (Node.NodeType = ntElement) then
        begin
          InsertTextBeforeXMLElement(s, ParentNode, Node);  
    
          if Assigned(Node.FirstChild) and (Node.FirstChild.NodeType = ntElement) then begin
            FormatElement(TElXMLDOMElement(Node), Level + 1);
    
            AddTextToXMLElement(s, TElXMLDOMElement(Node));
          end;
        end;
        
        Node := Node.NextSibling;
      end;
      if Level = 0 then
        AddTextToXMLElement(#10, ParentNode);
    end;
#24508
Posted: 04/09/2013 08:44:51
by Eugene Mayevski (EldoS Corp.)

Thank you for the code. Please remember to mark it with CODE tag or corresponding CODE button above the edit box.


Sincerely yours
Eugene Mayevski
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 1595 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!