Labels

Monday, June 3, 2013

Universal XML export

Few days ago I had a task to export some records (about 20 tables related) in XML.
I'd prefer to write some kind of universal export procedure, rather than proceed with every table.

So, at first, after running we should read the settings from the file, which will be located in the same location as the result export file. Every line in the file should be as follows: TableNo [Tab] FieldNo, meaning that this field should be exported to XML.
The procedure for reading this file should be like this:

ReadSettings()
DlgStatus.UPDATE(1, StatusReadSettings);
SettingFile.TEXTMODE(TRUE);
SettingFile.WRITEMODE(FALSE);
SettingFile.OPEN(ExportPath + 'mapping.txt');
REPEAT
  SettingFile.READ(SettingString);
  i := 1;
  CLEAR(Index);
  WHILE i <= STRLEN(SettingString) DO BEGIN
    Symbol := COPYSTR(SettingString,i,1);
    IF STRPOS('0123456789',Symbol) <> 0 THEN BEGIN
      IF (Index[1] = 0) THEN Index[1] := i;
      IF (Index[2] <> 0) AND (Index[3] = 0) THEN Index[3] := i;
    END ELSE BEGIN
      IF (Index[1] <> 0) AND (Index[2] = 0) THEN Index[2] := i;
    END;
    i += 1;
  END;
  EVALUATE(Mapping[1], DELCHR(COPYSTR(SettingString,Index[1],Index[2]-Index[1]),'<>', ' '));
  EVALUATE(Mapping[2], DELCHR(COPYSTR(SettingString,Index[3],STRLEN(SettingString)-Index[3]+1),'<>', ' '));
  ExportFields.INIT;
  ExportFields.TableNo := Mapping[1];
  ExportFields."No." := Mapping[2];
  ExportFields.INSERT;
UNTIL SettingFile.POS = SettingFile.LEN;
where ExportFields is the temporary table, based on the table 2000000041 Field.

Next, I've prepared the core function to export whatever record: