![]() |
IXMLDOMDocument formatieren
Hallo,
ich bearbeite mit einem IXMLDOMDocument2 Objekt eine XML Datei. im Programm füge ich immer neue Nodes hinzu und speicher anschließend das Dokument. Nur die angehängten Nodes sind in der Datei alle hintereinander gehängt und ist nicht wirklich gut von Hand zu editieren. Meine Frage ist nun, ob es ohne großen Aufwand möglich ist die Elemente ordentlich untereinanderzuhängen. Im Moment siehts so aus:
XML-Code:
Und so sollte es eigentlich aussehen:
<a>
[b]Content[/b][b]Content[/b][b]Content[/b]</a>
XML-Code:
hat da jemand eine Idee das ganze zur Laufzeit unkompliziert zu lösen?
<a>
[b]Content[/b] [b]Content[/b] [b]Content[/b] </a> |
Re: IXMLDOMDocument formatieren
Zitat:
Cheers, |
Re: IXMLDOMDocument formatieren
Delphi-Quellcode:
dürfte helfen (zumindestens wenn man die Datei erstellt, bzw. Nodes einfügt ... ich weiß jetzt nicht, ob dieses auch etwas bei vorhandenen Daten etwas bewirkt, wenn man die Datei vorher unformatiert eingelesen hat)
XML.Options := [doNodeAutoIndent];
ansonsten vermutlich nach dem Einlesen dieses FormatXmlData aufrufen und weiter diese Option nutzen, oder einfach vor dem Speichern. |
Re: IXMLDOMDocument formatieren
Hallo,
ich habe da eine eigene Funktion:
Delphi-Quellcode:
Gruß
procedure 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 IndentStr := IndentStr + IndentString; 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; xaromz |
Re: IXMLDOMDocument formatieren
Wow,
dankeschön für die Hilfreichen Sachen. |
Re: IXMLDOMDocument formatieren
Hi, ich habe auch das Problem mit dem Formatieren einer XML Datei!
Die Funktion: Zitat:
Dann hätte ich die Funktion Beautify ausprobiert. Da ist jedoch InsertTextNode & GetNesting unbekannt! Wo sind diese definiert? |
Re: IXMLDOMDocument formatieren
Hallo,
Zitat:
Delphi-Quellcode:
Ich hoffe, jetzt fehlt nichts mehr.
function 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 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 Parent.appendChild(Result) else Parent.insertBefore(Result, Parent.childNodes[Index]); end else Result := nil; end else Result := nil; end; Gruß xaromz |
Re: IXMLDOMDocument formatieren
:dancer:
Funktioniert nun Super!! Jetzt ist es nicht mehr in einer Wurst...! Danke! |
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:26 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz