首页 > Windows开发 > 详细

Delphi XE XML信息的读取

时间:2020-03-11 09:43:02      阅读:206      评论:0      收藏:0      [点我收藏+]
<?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;

最后,看一下运行结果:

技术分享图片

 

Delphi XE XML信息的读取

原文:https://www.cnblogs.com/redhat588/p/12460244.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!