Remove specific XML element in Delphi -
i have xml document looks 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 using delphi 7.
how can remove extensiondata elements in xml document this?
you can use ixmlnodelist.delete()
or ixmlnodelist.remove()
method remove nodes:
var root: ixmlnode; begin root := xmldocument1.documentelement; root.childnodes.delete('elementdata'); := 0 root.childnodes.count-1 root.childnodes[i].childnodes.delete('elementdata'); end;
var root, child, node: ixmlnode; begin root := xmldocument1.documentelement; node := root.childnodes.findnode('elementdata'); if node <> nil root.childnodes.remove(node); := 0 root.childnodes.count-1 begin child := root.childnodes[i]; node := child.childnodes.findnode('elementdata'); if node <> nil child.childnodes.remove(node); end; end;
if want remove elementdata
elements regardless of depth within document, recursive procedure can that:
procedure removeelementdata(node: ixmlnode); var root, child: ixmlnode; begin repeat until node.childnodes.delete('elementdata') = -1; := 0 node.childnodes.count-1 removeelementdata(node.childnodes[i]); end; end; begin removeelementdata(xmldocument1.documentelement); end;
Comments
Post a Comment