zoukankan      html  css  js  c++  java
  • Delphi XE XML信息的读取

    <?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;

    最后,看一下运行结果:

  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/redhat588/p/12460244.html
Copyright © 2011-2022 走看看