Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi OpenXML navigieren in TDomDocument (https://www.delphipraxis.net/114737-openxml-navigieren-tdomdocument.html)

Baeuerle 30. Mai 2008 11:09


OpenXML navigieren in TDomDocument
 
Hallo,

Delphi-Quellcode:
<?xml version="1.0" encoding="UTF-8" ?>
<liste>
 <item>
  <isactive>true</isactive>
  ...
 </item>
 <item>
  <isactive>false</isactive>
  ...
 </item>
</liste>

var xdoc: TDomDocument;
// Auslesen des Wertes "false" für das 2te item
xdoc.FindFirstChildElement.GetElementsByTagName('item').Item(1).ChildNodes.Item(0).ChildNodes.Item(0).NodeValue;
Ich denke das müsste doch komplett über den Namen ansprechbar sein???

Delphi-Quellcode:
// So irgendwie
xdoc.FindFirstChildElement.GetElementsByTagName('item').Item(1).GetElementsByTagName('isactive').NodeValue
Weiß jemand wie das am elegantesten gelöst wird? Vor allem wenn das XML-File noch tiefer geschachtelt wird.


thx

marabu 30. Mai 2008 16:11

Re: OpenXML navigieren in TDomDocument
 
Hi,

wenn ich das Manual richtig lese, dann kannst du eine XPathExpression verwenden um an alle Knoten zu gelangen:

Delphi-Quellcode:
var
  xp: TXPathExpression;
  doc: TDomDocumentXPath;
begin
  // ...
  xp := TXPathExpresion.Create(self);
  try
    xp.Expression := '//item/isactive';
    xp.ContextNode := doc.documentElement;
    if xp.Evaluate
      then ShowMessage(xp.ResultNode(0).NodeValue)
      else ShowMessage('rats');
  finally
    xp.Free;
  end;
end;
Getippt und nicht getestet.

Freundliche Grüße


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