我想学习xml的使用方法,想着个例子研究研究,谢谢

解决方案 »

  1.   

    http://www.nldelphi.com/cgi-bin/articles.exe/ShowArticle?ID=7066
    var
      xmlRoot: IXMLNode;
      xmlAdres: IXMLNode;
      xmlAdresItem: IXMLNode;
    begin
      XMLDocument.FileName := '.\XMLDocument1.xml';
      XMLDocument.Active := True;  try
        xmlRoot   := XMLDocument.DocumentElement;
        xmlAdres  := xmlRoot.ChildNodes.FindNode('adres');    while Assigned(xmlAdres) do
        begin
          xmlAdresItem  := xmlAdres.ChildNodes[0];      while Assigned(xmlAdresItem) do
          begin
            Memo.Lines.Add(xmlAdresItem.Text);
            xmlAdresItem  := xmlAdresItem.NextSibling;
          end;      Memo.Lines.Add('--------');
          xmlAdres  := xmlAdres.NextSibling;
        end;
      finally
        XMLDocument.Active := False;
      end;
    end;
      

  2.   

    http://www.swissdelphicenter.ch/torry/showcode.php?id=1285...dynamically build a menu from an XML file? 
    Author: Benjamin Heil  
    0 Comments to this tip [Write new comment] 
    [ Print tip ]     Tip Rating (13):   
           { 
      The following procedure allows you to build a menu from an XML file. 
      Special feature: You only need to specify the Name of the procedure which then 
      will be attached to a OnClick handler. 
      Note that the procedure must be declared as public. 
    } { 
      Mit folgender Prozedur kann man aus einem XML-File ein Menu 
      erstellen lassen (einfach im OnCreate aufrufen). 
      Besonderes Feature: Im XML-File gebt ihr nur den Namen der Prozedur an, 
      die dem OnClick-Ereignis zugewiesen werden soll. 
      Die einzige Einschränkung besteht darin, dass diese Prozedur 
      published sein muss. 
      Bindet einfach diese Prozedur in euer Hauptformular ein: 

    procedure TMainForm.CreateMenuFromXMLFile;   function Get_Int(S: string): Integer; 
      begin 
        Result := 0; 
        try 
          Result := StrToInt(S); 
        except 
        end; 
      end;   procedure AddRecursive(Parent: TMenuItem; Item: IXMLNode); 
      var 
        I: Integer; 
        Node: TMenuItem; 
        Child: IXMLNode; 
        Address: TMethod; 
      begin 
        Node := TMenuItem.Create(Parent); 
        if (Uppercase(Item.Attributes['CAPTION']) <> 'SEPERATOR') then 
        begin 
          Node.Caption := Item.Attributes['CAPTION']; 
          if (Uppercase(Item.Attributes['ID']) <> 'NONE') then 
          begin 
            Address.Code := MethodAddress(Item.Attributes['ID']); 
            Address.Data := Self; 
            if (Item.ChildNodes.Count - 1 < 0) then 
              Node.OnClick := TNotifyEvent(Address); 
          end; 
          if (Uppercase(Item.Attributes['SHORTCUT']) <> 'NONE') then 
            Node.ShortCut := TextToShortCut(Item.Attributes['SHORTCUT']); 
          Node.Checked := (Item.Attributes['CHECKED'] = '1'); 
        end 
        else 
          Node.Caption := '-'; 
        Node.Visible := (Item.Attributes['VISIBLE'] = '1');     if Parent <> nil then 
          Parent.Add(Node) 
        else 
          MainMenu.Items.Add(Node);     for I := 0 to Item.ChildNodes.Count - 1 do 
        begin 
          Child := item.ChildNodes[i]; 
          if (Child.NodeName = 'ENTRY') then 
            AddRecursive(Node, Child); 
        end; 
      end; 
    var 
      Root: IXMLMENUType; 
      Parent: TMenuItem; 
      I: Integer; 
      Child: IXMLNode; 
    begin 
      XMLDocument.FileName := ExtractFilePath(Application.ExeName) + XMLFile; 
      if not FileExists(XMLDocument.FileName) then 
      begin 
        MessageDlg('Menu-XML-Document nicht gefunden!', mtError, [mbOK], 0); 
        Halt; 
      end; 
      XMLDocument.Active := True;   Screen.Cursor := crHourglass; 
      try 
        Root := GetXMLMenu(XMLDocument); 
        Parent := nil;     for I := 0 to Root.ChildNodes.Count - 1 do 
        begin 
          Child := Root.ChildNodes[i]; 
          if (Child.NodeName = 'ENTRY') then 
            AddRecursive(Parent, Child); 
        end; 
      finally 
        Screen.Cursor := crDefault; 
      end; 
    end;