Hallo,
dieses Beispiel zeigt dir, wie du einen
XML-Knoten in einem Dokument per XPATH lokalisieren und seinen Level bestimmen kannst:
Delphi-Quellcode:
function FindNode(doc: IDOMDocument; const xpath: string;
var node: IDOMNode): Boolean;
begin
with doc.documentElement as IDOMNodeSelect do
node := selectNode(xpath);
Result := Assigned(node);
end;
function NodeLevel(node: IDOMNode): Integer;
begin
Result := -1;
while Assigned(node) do
begin
node := node.parentNode;
Inc(Result);
end;
end;
procedure TDemoForm.ButtonClick(Sender: TObject);
var
xpath: string;
node: IDOMNode;
begin
xpath := '//parent::node()[name = "Gruppe2"]';
if FindNode(XMLDocument.DOMDocument, xpath, node)
and Assigned(node.parentNode) then
begin
ShowMessage(Format('nodeLevel is %d', [NodeLevel(node)]));
node.ParentNode.removeChild(node);
Memo.Lines.Assign(XMLDocument.XML);
Memo.Lines.Text := XMLDocument.DocumentElement.XML;
end;
end;
Grüße vom marabu