Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi AddChild funktioniert nicht (https://www.delphipraxis.net/107094-addchild-funktioniert-nicht.html)

aegidos 22. Jan 2008 12:57


AddChild funktioniert nicht
 
Hallo,
Ich habe ein Problem, mein Programm soll eigentlich in eine existente XML-Datei einen Knoten einhängen tut dies aber nicht. Ich habe keine Ahnung woran das liegen kann.

Delphi-Quellcode:
procedure Programm.AddToValidate(text: String);
var
XMLDocument1: IXMLDocument;
iNode             : IXMLNode;
strDummy         :String;
b_flag : bool;
begin
     strDummy := 'C:\validate.xml';
     strDummy := StringReplace(strDummy, '/', '\', [rfReplaceAll, rfIgnoreCase]);
     if( FileExists(strDummy) = true) then
     begin
     XMLDocument1 := TXMLDocument.Create(strDummy);
     //XMLDocument1.Filename:=strDummy;
     XMLDocument1.Active := true;
     XMLDocument1.DocumentElement.AddChild('mat');
     b_flag := XMLDocument1.DocumentElement.HasChildNodes;

     iNode              := XMLDocument1.DocumentElement;
     XMLDocument1.DocumentElement.ChildNodes.FindNode( 'valid_serial' ).AddChild('materialnr');
     XMLDocument1.DocumentElement.ChildNodes.FindNode('valid_serial').ChildNodes.FindNode('materialnr').Text := text;
     XMLDocument1.SaveToFile('strDummy');
     end;
end;
In diese XML Datei soll die Materialnummer eingehängt werden:
Delphi-Quellcode:
<validate>
<valid_serial>
</valid_serial>
</validate>
Tausend Dank jetzt schonmal

angos 22. Jan 2008 13:47

Re: AddChild funktioniert nicht
 
Hi,

du hattest mehrere kleine Fehler drin. Dein Hauptproblem wird gewesen sein, dass dein XMLDocument keinen Owner hatte. Hier einmal der korrigierte Source:

Delphi-Quellcode:
procedure AddToValidate(text: string);
var
  XMLDocument1: TXMLDocument; // Fehler 1
  iNode: IXMLNode;
  strDummy: string;
  b_flag: bool;
begin
  strDummy := 'C:\validate.xml';
  strDummy := StringReplace(strDummy, '/', '\', [rfReplaceAll, rfIgnoreCase]);
  if (FileExists(strDummy) = true) then
  begin
    XMLDocument1 := TXMLDocument.Create(Application); // Fehler 2
    XMLDocument1.LoadFromFile(strDummy); // Fehler 2

    XMLDocument1.Active := True;
    XMLDocument1.DocumentElement.AddChild('mat');
    b_flag := XMLDocument1.DocumentElement.HasChildNodes;

    iNode := XMLDocument1.DocumentElement;
    XMLDocument1.DocumentElement.ChildNodes.FindNode('valid_serial').AddChild('materialnr');
    XMLDocument1.DocumentElement.ChildNodes.FindNode('valid_serial').ChildNodes.FindNode('materialnr').Text := text;
    XMLDocument1.SaveToFile(strDummy); // Fehler 3
  end;
end;

aegidos 23. Jan 2008 07:13

Re: AddChild funktioniert nicht
 
Fantastisch,
Danke für die schnelle und gute Hilfe. Es läuft nun einwandfrei und ich freu mich wie ein Schneekönig :-)
Danke Danke Danke!


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:46 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