Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi IXMLDOMDocument formatieren (https://www.delphipraxis.net/137232-ixmldomdocument-formatieren.html)

delphis spassbremse 16. Jul 2009 20:19


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:
<a>
  [b]Content[/b][b]Content[/b][b]Content[/b]</a>
Und so sollte es eigentlich aussehen:
XML-Code:
<a>
  [b]Content[/b]
  [b]Content[/b]
  [b]Content[/b]
</a>
hat da jemand eine Idee das ganze zur Laufzeit unkompliziert zu lösen?

mjustin 16. Jul 2009 20:24

Re: IXMLDOMDocument formatieren
 
Zitat:

Zitat von delphis spassbremse
Meine Frage ist nun, ob es ohne großen Aufwand möglich ist die Elemente ordentlich untereinanderzuhängen.

Die Funktion FormatXmlData aus der Unit xmldoc ist genau dazu gedacht.

Cheers,

himitsu 16. Jul 2009 20:41

Re: IXMLDOMDocument formatieren
 
Delphi-Quellcode:
XML.Options := [doNodeAutoIndent];
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)

ansonsten vermutlich nach dem Einlesen dieses FormatXmlData aufrufen und weiter diese Option nutzen,
oder einfach vor dem Speichern.

xaromz 16. Jul 2009 21:02

Re: IXMLDOMDocument formatieren
 
Hallo,

ich habe da eine eigene Funktion:
Delphi-Quellcode:
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;
Gruß
xaromz

delphis spassbremse 17. Jul 2009 13:59

Re: IXMLDOMDocument formatieren
 
Wow,

dankeschön für die Hilfreichen Sachen.

schwa226 18. Jul 2009 08:16

Re: IXMLDOMDocument formatieren
 
Hi, ich habe auch das Problem mit dem Formatieren einer XML Datei!

Die Funktion:
Zitat:

XML.Options := [doNodeAutoIndent];
ist bei meinem IXMLDOMDocument, msxml leider nicht vorhanden.

Dann hätte ich die Funktion Beautify ausprobiert.
Da ist jedoch InsertTextNode & GetNesting unbekannt!

Wo sind diese definiert?

xaromz 18. Jul 2009 08:45

Re: IXMLDOMDocument formatieren
 
Hallo,
Zitat:

Zitat von schwa226
Wo sind diese definiert?

:oops: in meiner Unit.
Delphi-Quellcode:
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;
Ich hoffe, jetzt fehlt nichts mehr.

Gruß
xaromz

schwa226 18. Jul 2009 08:48

Re: IXMLDOMDocument formatieren
 
:dancer:

Funktioniert nun Super!!

Jetzt ist es nicht mehr in einer Wurst...!

Danke!


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:25 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz