RAD Studio 10.2.3 測試√
示例:
<?xml version="1.0" encoding="UTF-8"?>
<Persons>
<test>測試内容</test>
<Person>
<Name>小印01</Name>
<Age>22</Age>
<Sex>男</Sex>
</Person>
<Person>
<Name>小印02</Name>
<Age>23</Age>
<Sex>男</Sex>
</Person>
</Persons>
結果:
代碼:
// uses Xml.XMLDoc, Xml.XMLIntf
procedure ReadXMlNode(AStrList: TStringList; ANode: IXMLNode);
var
i: Integer;
mNodeList: IXMLNodeList;
mNodeName: string;
mTmpStr: string;
begin
mTmpStr := '';
if not ANode.HasChildNodes then // 如果 mNode 沒有子節點
Exit;
mNodeList := ANode.ChildNodes;
for i := 0 to mNodeList.Count - 1 do
begin
mNodeName := mNodeList[i].NodeName;
if mNodeList[i].IsTextElement then // 如果是文本元素
begin
mTmpStr := mNodeName + ': ' + mNodeList[i].NodeValue;
AStrList.Add(mTmpStr);
end
else
if mNodeList[i].HasChildNodes then // 如果還有子節點
begin
ReadXMlNode(AStrList, mNodeList[i]);
end;
end;
end;
procedure TForm_Tips.Button_ReadXMLFileClick(Sender: TObject);
var
mFileName: string;
mDocument: IXMLDocument;
mMainNode: IXMLNode;
mStrList: TStringList;
begin
mFileName := ExtractFileDir(ParamStr(0)) + '\test.xml';
if not FileExists(mFileName) then
Exit;
mDocument := TXMLDocument.Create(nil);
mDocument.LoadFromFile(mFileName);
mMainNode := mDocument.DocumentElement;
mStrList := TStringList.Create;
ReadXMlNode(mStrList, mMainNode);
Memo_Log.Lines.Text := mStrList.Text;
mStrList.Free;
end;
一點點筆記,以便以後翻閱。