delphi XML简单处理

Posted jjw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi XML简单处理相关的知识,希望对你有一定的参考价值。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Xml.XMLDoc, Xml.xmldom, Xml.XMLIntf,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  IDoc: IXMLDocument;
  Root, N1, N2, N3: IXMLNode;
  I: Integer;
begin
  IDoc := TXMLDocument.Create(nil);
  IDoc.Active := True;
  Root := IDoc.AddChild(公司);
  for I := 1 to 9 do
  begin
    N1 := Root.AddChild(部门);
    N1.Attributes[Id] := I;
    N1.AddChild(名称).NodeValue := 部门_+I.ToString;
    N1.AddChild(主管).NodeValue := 主管_+I.ToString;
  end;
  IDoc.SaveToFile(d:\jjw.xml);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Doc: IXMLDocument;
  Node: IXMLNode;
  I: Integer;
begin
  Doc := TXMLDocument.Create(nil);
  Doc.LoadFromFile(d:\jjw.xml);
  Doc.Active := True;
  Node := Doc.DocumentElement;
  for I := 0 to Node.ChildNodes.Count-1 do
    Node.ChildNodes[I].AddChild(人数).NodeValue := IntToStr(I+1);
  Doc.SaveToFile(d:\jjw.xml);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  Doc: IXMLDocument;
  Node: IXMLNode;
  I: Integer;
begin
  Doc := TXMLDocument.Create(nil);
  Doc.LoadFromFile(d:\jjw.xml);
  Doc.Active := True;
  Node := Doc.DocumentElement;
  for I := 0 to Node.ChildNodes.Count-1 do
    Node.ChildNodes[I].ChildNodes.Delete(2);
  Doc.SaveToFile(d:\jjw.xml);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  Doc: IXMLDocument;
  Root, Node: IXMLNode;
  I: Integer;
begin
  Doc := TXMLDocument.Create(nil);
  Doc.LoadFromFile(d:\jjw.xml);
  Doc.Active := True;
  Root := Doc.DocumentElement;
  for I := 0 to Root.ChildNodes.Count-1 do
  begin
    Node := Root.ChildNodes[I];
    if Node.Attributes[Id]=3 then
      Node.ChildNodes[2].NodeValue := 33333;
  end;
  Doc.SaveToFile(d:\jjw.xml);
end;

end.

 

以上是关于delphi XML简单处理的主要内容,如果未能解决你的问题,请参考以下文章

Delphi直接读取XmL

一个关于Delphi XML处理单元的BUG

delphi 的TXMLDocument组件 DOMVendor属性

delphi 读取xml

Delphi多线程怎么处理?

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途