procedure TForm1.Beautify(
const XML: IXMLDOMDocument;
const IndentString: WideString = #9);
procedure InsertFormattingNode(
const Node: IXMLDOMNode;
const Len,
Index: Integer; Break: Boolean = True);
var
I: Integer;
IndentStr: WideString;
begin
for I := 1
to Len
do begin
IndentStr := IndentStr + IndentString;
end;
if Break
then
IndentStr := SLineBreak + IndentStr;
InsertTextNode(Node, IndentStr,
Index);
end;
procedure ProcessList(
const Node: IXMLDOMNode);
var
I: Integer;
Nesting: Integer;
begin
if not Assigned(Node)
then
Exit;
I := 0;
Nesting := GetNesting(Node);
while I < Node.childNodes.length
do
begin
if (Node.childNodes[I].nodeType = NODE_ATTRIBUTE)
or (Node.childNodes[I].nodeName = '
#text')
then
begin
Inc(I);
Continue;
end;
if I = 0
then
InsertFormattingNode(Node, Nesting + 1, I)
else
InsertFormattingNode(Node, 1, I, False);
Inc(I, 2);
InsertFormattingNode(Node, Nesting, I);
Inc(I);
end;
for I := 0
to Node.childNodes.length - 1
do
ProcessList(Node.childNodes[I]);
end;
begin
ProcessList(
XML.documentElement);
end;
function TForm1.GetNesting(
const Node: IXMLDOMNode): Integer;
var
Parent: IXMLDOMNode;
begin
Result := 0;
Parent := Node.parentNode;
while Assigned(Parent)
and (Parent.nodeType <> NODE_DOCUMENT)
do begin
Parent := Parent.parentNode;
Inc(Result);
end;
end;
function TForm1.InsertTextNode(
const Parent: IXMLDOMNode;
const Content: WideString;
const Index: Integer = -1): IXMLDOMNode;
var
XML: IXMLDomDocument;
begin
if Assigned(Parent)
then begin
XML := Parent.ownerDocument;
if Assigned(
XML)
then begin
Result :=
XML.createTextNode(Content);
if Index = -1
then begin
Parent.appendChild(Result)
end
else
Parent.insertBefore(Result, Parent.childNodes[
Index]);
end
else
Result :=
nil;
end
else
Result :=
nil;
end;