从IXMLDOMDocument2(Delphi)中删除属性

时间:2013-12-16 03:21:47

标签: xml delphi delphi-7

我有以下XML:

<TABLE Table="STUDENT">
    <FIELD fieldName="ID" fldType="INT"/>
    <FIELD fieldName="NAME" fldType="STRING"/>
    <FIELD fieldName="REDUNDANTFIELD" fldType="STRING"/>
    <FIELD fieldName="PHONENUMBER" fldType="STRING"/>
</TABLE>

我想删除fieldname属性为“REDUNDANTFIELD”的行。我该怎么办?

删除行后的预期输出

<TABLE Table="STUDENT">
    <FIELD fieldName="ID" fldType="INT"/>
    <FIELD fieldName="NAME" fldType="STRING"/>
    <FIELD fieldName="PHONENUMBER" fldType="STRING"/>
</TABLE>

任何人都可以帮我继续以下功能吗?

function RemoveAttributeRRow(myXML: IXMLDOMDocument2): IXMLDOMDocument2;
begin
  // my code here
  Result:= ???;
end;

1 个答案:

答案 0 :(得分:1)

procedure RemoveAttributeRRow(doc: IXMLDOMDocument2);
var
  node: IXMLDOMNode;
begin
  node := Doc.selectSingleNode('//FIELD[@fieldName=''REDUNDANTFIELD'']');
  if node<>nil then
    node.parentNode.removeChild(node);
end;

procedure TForm14.Button3Click(Sender: TObject);
var
  m: IXMLDOMDocument2;
begin
  m := CoDOMDocument.Create;
  m.load('c:\work\1.xml');
  RemoveAttributeRRow(m);
  m.save('c:\work\2.xml');
end;