IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bases de données Delphi Discussion :

Convertir un Dataset en XML standard


Sujet :

Bases de données Delphi

  1. #1
    Membre habitué Avatar de XeGregory
    Homme Profil pro
    Passionné par la programmation
    Inscrit en
    Janvier 2017
    Messages
    260
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Passionné par la programmation
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2017
    Messages : 260
    Points : 171
    Points
    171
    Par défaut Convertir un Dataset en XML standard
    Bonjour,

    Je cherche à convertir un TDataset (TClientDataSet) en XML standard, je sais que l’inverse c'est possible (Xml standard --> TDataSet) visa le (TXMLTransform).
    Maintenant (TDataSet --> Xml standard) j'ai aucune idée

    Merci.
    Vous ne pouvez pas faire confiance à un code que vous n'avez pas totalement rédigé vous-même.

  2. #2
    Membre habitué Avatar de XeGregory
    Homme Profil pro
    Passionné par la programmation
    Inscrit en
    Janvier 2017
    Messages
    260
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Passionné par la programmation
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2017
    Messages : 260
    Points : 171
    Points
    171
    Par défaut
    Effectivement cela est possible visa le TXMLDocument

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    procedure DataSetToXml(DataSet: TDataSet; XFileName: String);
    var
      I: Integer;
      Xml: TXMLDocument;
      Node, NodeStr: IXMLNode;
    begin
      Xml := TXMLDocument.Create(nil);
      try
        Xml.Active := True;
        Xml.DocumentElement := Xml.CreateElement('DataSet', EmptyStr);
     
        with DataSet do
        begin
          DisableControls;
          First;
     
          while not Eof do
          begin
            Node := Xml.DocumentElement.AddChild('Row');
     
            for I := 0 to Fields.Count - 1 do
            begin
              NodeStr := Node.AddChild(StringReplace(Fields[I].FieldName, ' ', '_', [rfReplaceAll, rfIgnoreCase]));
              NodeStr.Text := Fields[I].AsString;
            end;
     
            Next;
          end;
        end;
     
        Xml.SaveToFile(XFileName);
      finally
        FreeAndNil(Xml);
      end;
     
      DataSet.EnableControls;
    end
    Vous ne pouvez pas faire confiance à un code que vous n'avez pas totalement rédigé vous-même.

  3. #3
    Membre émérite
    Avatar de ALWEBER
    Homme Profil pro
    Expert Delphi
    Inscrit en
    Mars 2006
    Messages
    1 496
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Expert Delphi

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 496
    Points : 2 762
    Points
    2 762
    Billets dans le blog
    10
    Par défaut quelque pistes
    DFM
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
     
    object Form1: TForm1
      Left = 0
      Top = 0
      Caption = 'Form1'
      ClientHeight = 560
      ClientWidth = 541
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object DBGrid1: TDBGrid
        Left = 8
        Top = 80
        Width = 417
        Height = 201
        DataSource = DataSource1
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'Tahoma'
        TitleFont.Style = []
        OnTitleClick = DBGrid1TitleClick
      end
      object DBNavigator1: TDBNavigator
        Left = 136
        Top = 16
        Width = 240
        Height = 25
        DataSource = DataSource1
        TabOrder = 1
      end
      object Button1: TButton
        Left = 448
        Top = 111
        Width = 75
        Height = 25
        Caption = 'Sauve'
        TabOrder = 2
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 448
        Top = 80
        Width = 75
        Height = 25
        Caption = 'Charge'
        TabOrder = 3
        OnClick = Button2Click
      end
      object DBGrid2: TDBGrid
        Left = 8
        Top = 351
        Width = 417
        Height = 201
        DataSource = DataSource2
        TabOrder = 4
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'Tahoma'
        TitleFont.Style = []
        OnTitleClick = DBGrid1TitleClick
      end
      object Button3: TButton
        Left = 448
        Top = 295
        Width = 75
        Height = 25
        Caption = 'Transfert'
        TabOrder = 5
        OnClick = Button3Click
      end
      object Button4: TButton
        Left = 448
        Top = 328
        Width = 75
        Height = 25
        Caption = 'Sauve'
        TabOrder = 6
        OnClick = Button4Click
      end
      object DataSource1: TDataSource
        DataSet = ClientDataSet1
        Left = 80
        Top = 16
      end
      object ClientDataSet1: TClientDataSet
        Aggregates = <>
        Params = <>
        Left = 24
        Top = 16
      end
      object FDMemTable1: TFDMemTable
        FetchOptions.AssignedValues = [evMode]
        FetchOptions.Mode = fmAll
        ResourceOptions.AssignedValues = [rvSilentMode]
        ResourceOptions.SilentMode = True
        UpdateOptions.AssignedValues = [uvCheckRequired]
        UpdateOptions.CheckRequired = False
        AutoCommitUpdates = False
        Left = 24
        Top = 288
      end
      object DataSource2: TDataSource
        DataSet = FDMemTable1
        Left = 80
        Top = 288
      end
      object FDStanStorageXMLLink1: TFDStanStorageXMLLink
        Left = 208
        Top = 288
      end
      object FDStanStorageJSONLink1: TFDStanStorageJSONLink
        Left = 320
        Top = 296
      end
    end
    .pas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
     
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ClientDataSet1.MergeChangeLog ;
      ClientDataSet1.ApplyUpdates(-1) ;
      ClientDataSet1.SaveToFile('client.xml',dfXMLUTF8 );
    end;
     
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      ClientDataSet1.LoadFromFile ('client.xml');
    end;
     
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      FDMemTable1.CopyDataSet(ClientDataSet1,[coStructure, coRestart, coAppend]);
      FDMemTable1.Open;
    end;
     
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      FDMemTable1.SaveToFile('client1.xml',TFDStorageFormat.sfXML );
      FDMemTable1.SaveToFile('client1.json',TFDStorageFormat.sfJSON );
    end;
     
    procedure TForm1.DBGrid1TitleClick(Column: TColumn);
    var
      st1: string;
    begin
      st1 := Column.FieldName;
      with ClientDataSet1 do
      begin
        Close;
        IndexDefs.Clear;
        IndexFieldNames := st1;
        Open;
      end;
    end;
     
    end.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [DOM] convertir String en Document XML
    Par sebastien2222 dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 23/02/2007, 16h57
  2. [TXMLDocument] Documents XML Standard
    Par slimjoe dans le forum Delphi
    Réponses: 5
    Dernier message: 03/08/2006, 15h25
  3. Réponses: 8
    Dernier message: 16/03/2006, 09h32
  4. [XML / XLS ] Convertir un xls en XML
    Par NeHuS dans le forum XSL/XSLT/XPATH
    Réponses: 5
    Dernier message: 24/02/2006, 14h10
  5. [C#] [SQLSERVER2000] Dataset et Xml
    Par farfadet dans le forum ASP.NET
    Réponses: 11
    Dernier message: 16/03/2004, 16h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo