各位,我刚接触XML,对此技术很感兴趣,也觉得其应用前景很好!
可不可以给一些具体的应用实例,最好有Delphi源代码!

解决方案 »

  1.   

    应用实例系列1:// --------------------------------------------------------- //
    // Name:        ADOXMLFromDataSet(Source: TDataSet): String;
    // Description: Transfer the specified dataset into the XML
    //   document which supplied by microsoft's ADO;
    // Input Para:  1) Source: Dataset -- Original dataset
    // Output Par:  1) String -- XML document's string;
    // Comment:     1) 把数据集转换为ADO支持的XML格式;
    //              2) 注意xml文档中的microsoft的name-space;
    // --------------------------------------------------------- //
    function ADOXMLFromDataSet(Source: TDataSet): String;
    const
      BoolToStrMAP: array[boolean] of string = ('true','false');
    var
      FieldIndex: Integer;
      slst: TStringList;
    begin
      result := '';  slst := TStringList.Create;
      try
        // ADO XML Prolog -- Note is microsoft's type!!!
        slst.Add('<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"');
        slst.Add('xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"');
        slst.Add('xmlns:rs="urn:schemas-microsoft-com:rowset"');
        slst.Add('xmlns:z="#RowsetSchema">');    // Schema
        slst.Add('<s:Schema id="RowsetSchema">');
        slst.Add('<s:ElementType name="row" content="eltOnly" rs:updatable="true">');    // Loop through each of the Fields in the DataSet
        for FieldIndex := 0 to Source.FieldCount-1 do
        begin
          slst.Add(Source.Fields[FieldIndex].FieldName + '="' + Source.Fields[FieldIndex].AsString + '" ');
          slst.Add('<s:AttributeType name="' + Source.Fields[FieldIndex].FieldName + '" rs:number="' + IntToStr(FieldIndex) + '" rs:write="' + BoolToStrMAP[Source.Fields[FieldIndex].ReadOnly] + '">');
          slst.Add('<s:datatype dt:type="' + FieldTypeToADOString(Source.Fields[FieldIndex].DataType) + '" dt:maxLength="' + IntToStr(Source.Fields[FieldIndex].Size) + '"');
          slst.Add(' rs:maybenull="' + BoolToStrMAP[Source.Fields[FieldIndex].Required] + '"/>');
          // rs:precision='0' rs:long='true'
          slst.Add('</s:AttributeType>');
        end;    slst.Add('<s:extends type="rs:rowbase"/>');
        slst.Add('</s:ElementType>');
        slst.Add('</s:Schema>');    // Data
        slst.Add('<rs:data>');
        slst.Add('<rs:insert>');    while not Source.EOF do
        begin
          // Generic tag identifying new record
          slst.Add('<z:row ');      // Loop through each of the Fields in the DataSet
          for FieldIndex := 0 to Source.FieldCount-1 do
            // Add the Param for the Field
            slst.Add(Source.Fields[FieldIndex].FieldName + '="' + Source.Fields[FieldIndex].AsString + '" ');      // Close record tag
          slst.Add(' />');
          // Go to the next record
          Source.Next;
        end;    // while    slst.Add('</rs:insert>');
        slst.Add('</rs:data>');
        slst.Add('</xml>');    // return the result
        result := slst.Text;
      finally
        slst.free;
      end;
    end;
      

  2.   

    补充:实例1用到的一个函数!// --------------------------------------------------------- //
    // Name:        FieldTypeToADOString(p_FieldType: TFieldType): string;
    // Description: 把各种数据格式(Delphi supported)转换成ADO字符串;
    // Input Para:  1) p_FieldType: TFieldType;
    // Output Par:  1) String -- only has value 'string';
    // Comment:
    // --------------------------------------------------------- //
    function FieldTypeToADOString(p_FieldType: TFieldType): string;
    begin
      { TODO : Real values }
      case p_FieldType of
        ftUnknown, ftString, ftSmallint, ftInteger, ftWord,
        ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime,
        ftBytes, ftVarBytes, ftAutoInc, ftBlob, ftMemo, ftGraphic, ftFmtMemo,
        ftParadoxOle, ftDBaseOle, ftTypedBinary, ftCursor, ftFixedChar, ftWideString,
        ftLargeint, ftADT, ftArray, ftReference, ftDataSet, ftOraBlob, ftOraClob,
        ftVariant, ftInterface, ftIDispatch, ftGuid : result := 'string';
      end;
    end;
      

  3.   

    实例1应定名为:XML和数据库的交互!现补充如下:// --------------------------------------------------------- //
    // Name:        XMLFromDataSet(Source: TDataSet): String;
    // Description: Transfer the specified dataset into the XML
    //   document which self-definition;
    // Input Para:  1) Source: Dataset -- Original dataset
    // Output Par:  1) String -- XML document's string;
    // Comment:     1) 把数据集转换为自定义的XML格式;
    // --------------------------------------------------------- //
    function XMLFromDataSet(Source: TDataSet): String;
    var
      FieldIndex: Integer;
      slst: TStringList;
    begin
      result := '';  slst := TStringList.Create;
      try
        // Simple XML header
        slst.Add('<?xml version = "1.0" ?>');    // Generic name for set of all records
        slst.Add('<RecordSet tablename="' + Source.Name + '">');    while not Source.EOF do
        begin
          // Generic tag identifying new record
          slst.Add('<Record>');      // Loop through each of the Fields in the DataSet
          for FieldIndex := 0 to Source.FieldCount-1 do
            // Generate the XML of the data
            slst.Add('<' + Source.Fields[FieldIndex].FieldName + '>' + Source.Fields[FieldIndex].AsString + '</' + Source.Fields[FieldIndex].FieldName + '>');      // Closing record tag
          slst.Add('</Record>');
          // Go to the next record
          Source.Next;
        end;    // while    slst.Add('</RecordSet>');
        // return the result
        result := slst.Text;
      finally
        slst.free;
      end;
    end;