Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   XMLDocument zur Laufzeit -> Exception (https://www.delphipraxis.net/185202-xmldocument-zur-laufzeit-exception.html)

amigage 21. Mai 2015 15:12

XMLDocument zur Laufzeit -> Exception
 
Hallo,

ich habe ein Problem beim Einlesen vom XML Daten aus einem Stream.
Lege ich die XMLDocument Komponente auf eine Form ("XMLDoc") lassen sich die Inhalte problemlos auslesen:

Delphi-Quellcode:
function LoadWebData(xmlResponse : TMemoryStream) : boolean;
var
  StartItemNode: IXMLNode;
begin
  Result := true;

  try
    XMLDoc.LoadFromStream(xmlResponse);
  except;
    Result := false;
    exit;
  end;
  XMLDoc.Active := True;

  try
    StartItemNode := XMLDoc.DocumentElement.ChildNodes.FindNode('response') ;
  except
  end;
end;
Möchte ich die Funktionalität jedoch auslagern und das XMLDocument zu Laufzeit erzeugen, dann wird beim Abfragen der Struktur eine Exception ausgeworfen.

Delphi-Quellcode:
function LoadWebData(xmlResponse : TMemoryStream) : boolean;
var
  XMLDoc : TXMLDocument;
  StartItemNode: IXMLNode;
begin
  Result := true;

  XMLDoc := TXMLDocument.Create(NIL);
  try
    XMLDoc.DOMVendor := DOMVendors.Find('Omni XML');
    XMLDoc.Options := [doAttrNull,doAutoPrefix,doNamespaceDecl];

    try
      XMLDoc.LoadFromStream(xmlResponse);
    except;
      Result := false;
      exit;
    end;
    XMLDoc.Active := True;

    try
      StartItemNode := XMLDoc.DocumentElement.ChildNodes.FindNode('response') ; // <= Exception
    except
    end;
// ....
  finally
    XMLDoc.Active := false;
    XMLDoc := NIL;
  end;
end;
Was mache ich hier falsch? Habe ich vergessen, eine Eigenschaft zu setzen?

Vielen Dank, für jeden Hinweis.

Bernhard Geyer 21. Mai 2015 15:14

AW: XMLDocument zur Laufzeit -> Exception
 
Wurde der Stream auch wieder an den Anfang zurück gesetzt an die stelle an der die XML-Daten kommen. Bei LoadFromStream wird oft an der stelle das lesen begonnen an der gerade der Stream steht.

amigage 21. Mai 2015 16:11

AW: XMLDocument zur Laufzeit -> Exception
 
Hallo,

Du meinst
Delphi-Quellcode:
xmlResponse.Position := 0;
vor XMLDoc.LoadFromStream?
Nein, das bringt leider auch nichts.

Neutral General 21. Mai 2015 16:12

AW: XMLDocument zur Laufzeit -> Exception
 
Tipp des Jahrhunderts: Erstelle nie ein TXMLDocument mit Owner = nil.
Dadurch erhält man unter Umständen komische, nicht nachvollziehbare Exceptions.

Im Zweifelsfall erstell dir eine temporäre TComponent-Instanz die du dem constructor von TXMLDocument mitgibst.
Hauptsache nicht nil!

Achja: Du musst das xml Dokument im finally wieder freigeben. Auf nil setzen reicht nicht!

himitsu 21. Mai 2015 16:25

AW: XMLDocument zur Laufzeit -> Exception
 
In XMLDoc.ParseOptions ist nichts mit Async aktiv?



Ach ja, entweder
Delphi-Quellcode:
var XMLDoc : TXMLDocument;  
XMLDoc.Free;
oder
Delphi-Quellcode:
var XMLDoc : IXMLDocument;  
XMLDoc := NIL;

amigage 21. Mai 2015 16:35

AW: XMLDocument zur Laufzeit -> Exception
 
Zitat:

Zitat von Neutral General (Beitrag 1302483)
Tipp des Jahrhunderts: Erstelle nie ein TXMLDocument mit Owner = nil.
Dadurch erhält man unter Umständen komische, nicht nachvollziehbare Exceptions.

Im Zweifelsfall erstell dir eine temporäre TComponent-Instanz die du dem constructor von TXMLDocument mitgibst.
Hauptsache nicht nil!

Das scheint es in der Tat zu sein. Aber was meinst Du mit "temporäre TComponent-Instanz"?

@himitsu: Danke für die Erklärung des Unterschieds :thumb:

Neutral General 21. Mai 2015 16:38

AW: XMLDocument zur Laufzeit -> Exception
 
Sowas halt (falls du wie gesagt irgendwas anderes rumfliegen hast, dann kannst du das stattdessen auch übergeben und musst keine temporäre Instanz erstellen):

Delphi-Quellcode:
function LoadWebData(xmlResponse : TMemoryStream) : boolean;
var
  XMLDoc : TXMLDocument;
  StartItemNode: IXMLNode;
  dummyComp: TComponent;
begin
  Result := true;
 
  dummyComp := TComponent.Create(nil); // <--
  try
    XMLDoc := TXMLDocument.Create(dummyComp); // <--
    try
      XMLDoc.DOMVendor := DOMVendors.Find('Omni XML');
      XMLDoc.Options := [doAttrNull,doAutoPrefix,doNamespaceDecl];

      try
        XMLDoc.LoadFromStream(xmlResponse);
      except;
        Result := false;
        exit;
      end;
      XMLDoc.Active := True;

      try
        StartItemNode := XMLDoc.DocumentElement.ChildNodes.FindNode('response') ; // <= Exception
      except
      end;
  // ....
    finally
      XMLDoc.Active := false;
      XMLDoc := NIL;
    end;
  finally
    dummyComp.Free;
  end;
end;

amigage 21. Mai 2015 16:41

AW: XMLDocument zur Laufzeit -> Exception
 
:thumb: :thumb: Super, besten Dank. Das wars. Man lernt nie aus :idea:

Dejan Vu 22. Mai 2015 07:10

AW: XMLDocument zur Laufzeit -> Exception
 
Könnte man nicht vielleicht doch den leeren 'except' Block entfernen? Das ist ja grauenvoll.


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