我想了解一下DELPHI使用XML的方法,请问哪位朋友可以提供这方面的例子或者下载地址,谢谢。

解决方案 »

  1.   

    好像我还没有看见哪个专门有这个的,你在csdn delphi 上搜一下xml,看大家都是怎么说的,怎么用的?
      

  2.   

    xml和delhi的结合在数据库连接上的应用和数据传输等方面有着很重要的作用!unit concfg;interfaceuses
      Windows, Messages, SysUtils, Classes,IcXMLStrings, IcXMLParser;type
      TConcfg = class
        applicationname: string;
        provider: string;
        connectTimeout: string;
        datasource: string;
        initialCatalog: string;
        userid: string;
        version: string;
        password: String;
        function GetConnectString: string;
        constructor Create(AFilename: string);
      end;implementationconstructor TConcfg.Create(AFilename: string);
    var
      text: TIcXMLText;
      ele: TIcXMLElement;
      par: TIcXMLParser;
      doc : TIcXMLDocument;
    begin
      par := TIcXMLParser.Create(nil);
      doc := TIcXMLDocument.Create;
      //doc.SetEncoding('UTF-8');
      try
        par.Parse(AFilename,doc);
        ele := doc.GetDocumentElement;
        version := ele.GetAttribute('version');
        ele := ele.GetFirstChild;
        while ele <> nil do
        begin
          if (ele.TagName = 'ApplicationName') then
            if ele.HasCharData then
            begin
              text := ele.GetFirstCharData;
              applicationname := trim(text.GetValue);
            end;
          if (ele.TagName = 'Provider') then
            if ele.HasCharData then
            begin
              text := ele.GetFirstCharData;
              provider := trim(text.GetValue);
            end;
          if (ele.TagName = 'DataSource') then
            if ele.HasCharData then
            begin
              text := ele.GetFirstCharData;
              datasource := trim(text.GetValue);
            end;
          if (ele.TagName = 'InitialCatalog') then
            if ele.HasCharData then
            begin
              text := ele.GetFirstCharData;
              InitialCatalog := trim(text.GetValue);
            end;
          if (ele.TagName = 'UserID') then
            if ele.HasCharData then
            begin
              text := ele.GetFirstCharData;
              UserID := trim(text.GetValue);
            end;
          if (ele.TagName = 'Password') then
            if ele.HasCharData then
            begin
              text := ele.GetFirstCharData;
              Password := trim(text.GetValue);
            end;
          ele := ele.NextSibling;
        end;
        if length(Trim(UserID)) = 0 then
          UserID := 'sa';
        if length(Trim(Provider)) = 0 then
          Provider := 'SQLOLEDB.1';
        if length(Trim(initialcatalog)) = 0 then
          initialcatalog := 'hszxdata';
        if length(Trim(DataSource)) = 0 then
          DataSource := '192.168.0.3';
      finally
        doc.clear;
        doc.Free;
      end;
    end;function TConcfg.GetConnectString: string;
    begin
        result := 'Provider=' + provider + ';Password=' + password +
                  ';Persist Security Info=True;User ID=' + userid +
                  ';Initial Catalog=' + initialcatalog +
                  ';Data Source=' + DataSource;
    end
    end;
    刚刚看到的。
      

  3.   

    不了解xml,正在怀疑它的用处
      

  4.   

    这个例子看看
    unit test;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc, Grids;type
      TForm1 = class(TForm)
        add: TButton;
        XMLDocument1: TXMLDocument;
        Memo1: TMemo;
        view: TButton;
        addchild: TButton;
        order: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        Edit3: TEdit;
        StringGrid1: TStringGrid;
        Button2: TButton;
        procedure addClick(Sender: TObject);
        procedure viewClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure addchildClick(Sender: TObject);
        procedure orderClick(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    var
     current:Integer;procedure TForm1.addClick(Sender: TObject);
    var Clinic: IXMLNode; 
    begin 
    current := 0; 
    Clinic := XMLDocument1.DocumentElement.ChildNodes[current];
    Memo1.Lines.Clear;
     Clinic.ChildNodes['Title'].Text := 'HOT: ' + Clinic.ChildNodes['Title'].Text;
     Memo1.Lines.Add(Clinic.ChildNodes['Title'].Text);
     Memo1.Lines.Add(Clinic.ChildNodes['Date'].Text);
     Memo1.Lines.Add(Clinic.ChildNodes['Topics'].Text);
    end;
    procedure TForm1.viewClick(Sender: TObject);
     var Clinic: IXMLNode;beginInc(current);
    try
    Clinic := XMLDocument1.DocumentElement.ChildNodes[current];
    Memo1.Lines.Clear;
    Memo1.Lines.Add(Clinic.ChildNodes['Title'].Text);
    Memo1.Lines.Add(Clinic.ChildNodes['Date'].Text);
    Memo1.Lines.Add(Clinic.ChildNodes['Topics'].Text);
    except
    on E: Exception do
    Memo1.Lines.Add(E.Message)
    end
    end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    current:=-1;
    end;procedure TForm1.addchildClick(Sender: TObject);
     var Clinic: IXMLNode;
    begin
     Clinic := XMLDocument1.DocumentElement.AddChild('Clinic');
     Clinic.ChildNodes['Title'].Text := 'Title';
    Clinic.ChildNodes['Date'].Text := 'Date';
    Clinic.ChildNodes['Topics'].Text := 'Topics';
     end;procedure TForm1.orderClick(Sender: TObject);
    var
      ValueNode: IXMLNode;
      XmlTreeNode: IXMLNode;
    begin
      //XMLDocument1.LoadFromFile('Test.xml');
      //XMLDocument1.Active := True;
        XMLDocument1.Create('test.xml');
    //    XMLDocument1.s
      XmlTreeNode:=XMLDocument1.Node;
      XmlTreeNode := XMLDocument1.DocumentElement.AddChild('order');
      ValueNode := XmlTreeNode.AddChild('orderID');
      ValueNode.Text := Edit1.Text;
      ValueNode := XmlTreeNode.AddChild('orderName');
      ValueNode.Text := Edit1.Text;
      ValueNode := XmlTreeNode.AddChild('DigitalSign');
      ValueNode.Text := Edit2.Text;
      ValueNode := XmlTreeNode.AddChild('ApplyId');
       ValueNode.Text := Edit2.Text;
    //  XMLDocument1.Node.
      XMLDocument1.SaveToFile('Test.xml');  XmlTreeNode.ParentNode.ChildNodes.Delete(XmlTreeNode.ParentNode.ChildNodes.IndexOf(XmlTreeNode));
       XMLDocument1.SaveToFile('Test2.xml');
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i: integer;
      ChildCount: integer;
      xn: IXMLNode;//XML&Icirc;&Auml;&micro;&micro;&micro;&Auml;&Ecirc;÷&frac12;á&micro;&atilde;
      xnOrder: IXMLNode;
    begin
     // try
        XMLDocument1.LoadFromFile('test.xml');
    ChildCount := XMLDocument1.DocumentElement.ChildNodes.Count;
    //&frac14;&AElig;&Euml;&atilde;×&Oacute;&frac12;&Uacute;&micro;&atilde;&cedil;&ouml;&Ecirc;&yacute;
        for i := 0 to ChildCount - 1 do
        begin
          xn := XMLDocument1.DocumentElement.ChildNodes[i];
        //&frac14;&Ugrave;&Eacute;èx.xml&Ouml;&ETH;&Oacute;&ETH;OrderId&iexcl;&cent;orderName&iexcl;&cent;ApplyId&Egrave;&yacute;&cedil;&ouml;×&Oacute;&frac12;&Uacute;&micro;&atilde;&iexcl;&pound;
        //&Iuml;&Acirc;&Atilde;&aelig;&micro;&Auml;&Oacute;&iuml;&frac34;&auml;&Icirc;&ordf;&Egrave;&iexcl;&cedil;&Atilde;×&Oacute;&frac12;&Uacute;&micro;&atilde;&micro;&Auml;&Auml;&Uacute;&Egrave;&Yacute;...
         Edit1.text := xn.ChildNodes['OrderId'].Text;
          Edit2.text := xn.ChildNodes['orderName'].Text;
          Edit3.text := xn.ChildNodes['ApplyId'].Text;
         end;
     for i:=0 to ChildCount - 1 do
        begin
          xn := XMLDocument1.DocumentElement.ChildNodes[i];
          StringGrid1.Cols[0].Add(xn.ChildNodes['bianhao'].Text);
          StringGrid1.Cols[1].Add(xn.ChildNodes['name'].Text);
          StringGrid1.RowCount := StringGrid1.RowCount + 1;
        end;
       end;procedure TForm1.Button2Click(Sender: TObject);
    var
      NewStock: IXMLNode;
      ValueNode: IXMLNode;
    begin
      NewStock := XMLDocument1.DocumentElement.AddChild('stock');
      NewStock.Attributes['exchange'] := 'NASDAQ';
      ValueNode := NewStock.AddChild('name');
      ValueNode.Text := 'Cisco Systems';
      ValueNode:= NewStock.AddChild('price');
      ValueNode.Text := '62.375';
      ValueNode := NewStock.AddChild('symbol');
      ValueNode.Text := 'CSCO';
      ValueNode := NewStock.AddChild('shares');
      ValueNode.Text := '25';
     XMLDocument1.SaveToFile('Test2.xml');
    end;end.
      

  5.   

    问一下
    要用IXMLNode应该要装哪个东西啊