方法1:使用TXMLDocument
参考引用:Delphi 与 XML 示例(直接利用IXMLDocument)-木庄网络博客 (muzhuangnet.com)
uses xmldom, msxmldom, XMLIntf, XMLDoc;
procedure TForm1.btn9Click(Sender: TObject);
var
sXML,nodeValue,attributeValue: string;
xml: IXMLDocument;
rootNode,resultnode,infoNode: IXMLNode;
begin
//从文件读取
xml := TXMLDocument.Create(nil);
try
xml.LoadFromFile('c:\1.xml');
finally
xml := nil
end;
//从xml字符串读取
try
sXML:= memo1.Text;
xml := LoadXMLData(sXML);
rootNode := xml.ChildNodes.FindNode('root');
resultnode := rootNode.ChildNodes.FindNode('result');
infoNode := resultnode.ChildNodes.FindNode('info');
nodeValue := infoNode.GetNodeValue; //***** 取结点值 *****
attributeValue := infoNode.Attributes['execute_flag']; //***** 取结点属性 *****
ShowMessage(nodeValue);
ShowMessage(attributeValue);
finally
xml := nil;
end;
end;