Enregister un flux XMLDOMs en conservant le formatage

// sauvegarde de nodes avec CR (type visuel IE5)
procedure SaveXmlWithCR( XmlDomNode: IXMLDOMNODE; FileName: String );
var
XML: String;
begin
ForceDirectories(ExtractFilePath(FileName));
XML := XmlWithCR(XmlDomNode);
With TFileStream.Create(FileName, fmCreate) do
try
WriteBuffer(Pointer(XML)^, Length(XML));
finally
Free;
end;
end;

// Transformation de nodes avec CR (type visuel IE5)
function XmlWithCR(XmlDomNode: IXMLDOMNODE;
Dec: Integer=0): String;
var
i: Integer;
ChildNodeText: boolean;
begin
Result := '';
Case XmlDomNode.nodeType of
NODE_DOCUMENT_FRAGMENT:
for i := 0 to XmlDomNode.childNodes.length do
Result := Result + XmlWithCR( XmlDomNode.childNodes[i], Dec);
NODE_TEXT:
If XmlDomNode.parentNode.childNodes.length > 1 then
Result := StringOfChar(#9, Dec) + XmlDomNode.xml + #13#10
Else
Result := Trim(XmlDomNode.xml);
NODE_ELEMENT:
begin
Result := StringOfChar( #9, Dec) + '<' + XmlDomNode.nodeName;
for i := 0 to Pred(XmlDomNode.attributes.length) do
Result := Result+ ' '+XmlDomNode.attributes[i].xml;

If Not XmlDomNode.hasChildNodes then
Result := Result + '/>'+ #13#10
Else
Begin
ChildNodeText := (XmlDomNode.childNodes.length = 1) and
(XmlDomNode.childNodes[0].nodeType =
NODE_TEXT);
if ChildNodeText then
Result := Result + '>'
else
Result := Result + '>'+ #13#10;
for i := 0 to Pred(XmlDomNode.childNodes.length) do
Result := Result + XmlWithCR(XmlDomNode.childNodes[i],
Dec+1);
If not ChildNodeText then
Result := Result + StringOfChar( #9, Dec);
Result := Result + ''+#13#10;

End;
end;
NODE_PROCESSING_INSTRUCTION:
begin
Result := StringOfChar( #9, Dec)+ XmlDomNode.xml + #13#10;
Result := Result + XmlWithCR( XmlDomNode.nextSibling, Dec);
end;
NODE_DOCUMENT:
Result := XmlWithCR( XmlDomNode.FirstChild, Dec );
NODE_ENTITY, NODE_ATTRIBUTE:;
else
Result := StringOfChar( #9, Dec)+ XmlDomNode.xml + #13#10;
end;
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour28/01/2002
VisualisationVu 1665 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Internet
Auteur de l'article
SwissDelphiCenter SwissDelphiCenter
connecté le 02/11/2011
329 articles dans la section
Contacter l'auteur