0

I have an xml document looks like this:

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ExtensionData />
  <Name>ali</Name>
  <Age>37</Age>
  <Father>
    <ExtensionData />
    <Name>I</Name>
    <Age>72</Age>
  </Father>
  <Mother>
    <ExtensionData />
    <Name>M</Name>
    <Age>62</Age>
  </Mother>
</Person>

I am using Delphi 7.

How can I remove all ExtensionData elements in XML document like this?

4
  • Why XML library are you using? Commented Jun 13, 2015 at 14:36
  • He's probably using the default TXMLDocument wrapper. Commented Jun 13, 2015 at 15:23
  • Yes, I'm using TXMLDocument. Commented Jun 13, 2015 at 15:29
  • 2
    ExtensionData is an element, not an attribute, and in this example it has no attributes of its own, either. It is important to get the terminology correct because elements and attributes are accessed in different ways. Commented Jun 13, 2015 at 16:03

1 Answer 1

4

You can use the IXMLNodeList.Delete() or IXMLNodeList.Remove() method to remove nodes:

var
  Root: IXMLNode;
begin
  Root := XMLDocument1.DocumentElement;
  Root.ChildNodes.Delete('ElementData');
  for I := 0 to Root.ChildNodes.Count-1 do
    Root.ChildNodes[I].ChildNodes.Delete('ElementData');
end;

var
  Root, Child, Node: IXMLNode;
begin
  Root := XMLDocument1.DocumentElement;
  Node := Root.ChildNodes.FindNode('ElementData');
  if Node <> nil then Root.ChildNodes.Remove(Node);
  for I := 0 to Root.ChildNodes.Count-1 do
  begin
    Child := Root.ChildNodes[I];
    Node := Child.ChildNodes.FindNode('ElementData');
    if Node <> nil then Child.ChildNodes.Remove(Node);
  end;
end;

If you want to remove all ElementData elements regardless of their depth within the document, a recursive procedure can do that:

procedure RemoveElementData(Node: IXMLNode);
var
  Root, Child: IXMLNode;
begin
  repeat until Node.ChildNodes.Delete('ElementData') = -1;
  for I := 0 to Node.ChildNodes.Count-1 do
    RemoveElementData(Node.ChildNodes[I]);
  end;
end;

begin
  RemoveElementData(XMLDocument1.DocumentElement);
end;
Sign up to request clarification or add additional context in comments.

5 Comments

After removing a node, I'm saving the document using IXMLDocument's SaveToFile procedure but it writes an empty line for each removed node. Any idea about how to avoid this? (I'm testing on Delphi 2007)
@ExDev that happens when the original document has whitespace text nodes (line breaks, etc) between element nodes, such as if you enable the poPreserveWhiteSpace flag in the ParseOptions property when loading the document, and then you do not removing those extra text nodes when removing element nodes.
I'm creating a new document from scratch. For readability reasons, I've added doNodeAutoIndent to the document's Options. Then I'm removing a node using the Remove function, after doing this, what should I do in order to remove the empty line? example
@ExDev indentation is done using text nodes. Like I said, just remove the extra text nodes that preceed/follow the element node you are removing. They are children of the same parent node.
You're right, I've removed the extra text nodes and the empty line is disappeared. I would never have thought that indentation was done by adding "fake" nodes. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.