<?xml version="1.0" encoding="utf-8"?> <ConString> <Item> <Name/> <Type>C</Type> <Value> <Option>3301</Option> </Value> </Item> <Item> <Name/> <Type>C</Type> <Value> <Option>20200307001</Option> </Value> </Item> <Item> <Name/> <Type>C</Type> <Value> <Option>20200307</Option> </Value> </Item> <Item> <Name/> <Type>C</Type> <Value> <Option>201</Option> </Value> </Item> <Item> <Name/> <Type>C</Type> <Value> <Option>110100970</Option> <Option>110100970</Option> </Value> </Item> <Item> <Name/> <Type>C</Type> <Value> <Option>后门上车踏板L</Option> <Option>后门上车踏板L</Option> </Value> </Item> <Item> <Name/> <Type>C</Type> <Value> <Option>CW733538</Option> <Option>CW733538</Option> </Value> </Item> <Item> <Name/> <Type>N</Type> <Value> <Option>100</Option> <Option>200</Option> </Value> </Item> <Item> <Name/> <Type>N</Type> <Value> <Option>0</Option> <Option>0</Option> </Value> </Item> </ConString>
以上为XML信息,如何读取Option后的内容呢?
procedure TMainForm.ReadXml(Node: IXMLNode; var Params: string); var NodeList: IXMLNodeList; strName: string; i: Integer; begin if not Node.HasChildNodes then Exit; NodeList := Node.ChildNodes; for i := 0 to NodeList.Count - 1 do begin strName := NodeList[i].NodeName; if NodeList[i].IsTextElement then //如果是元素 begin if NodeList[i].NodeName = ‘Option‘ then Params := Params + NodeList[i].NodeValue + #13#10; end else if NodeList[i].HasChildNodes then //如果有子节点 begin ReadXml(NodeList[i], Params); end; end; end;
procedure TMainForm.btn4Click(Sender: TObject); var node: IXMLNode; ParamsStr: string; var LDocument: IXMLDocument; var Paramslist: TStringList; begin LDocument := TXMLDocument.Create(nil); LDocument.LoadFromXML(mmoxml.Text); node := LDocument.DocumentElement; ReadXml(node, ParamsStr); Paramslist := TStringList.Create; try Paramslist.Text := ParamsStr; //把数据传成数组 ShowMessage(Paramslist.Text); finally Paramslist.Free; end; end;
最后,看一下运行结果: