Xml

  • 31 May

    How to convert from XML to JSON format

    Q: We receive some files in XML format. Could you please confirm, if Limagito tool can convert XML files to JSON format? If yes, Do we have any limitations while converting the file type? Please let us know if you need any additional information.

    A: We added an option in version v2023.5.31.0 to achieve this.

    • Our Source will be a Windows folder

    limagito file mover Windows folder as source

    • We added the following include Filename filter because we only want to scan for *.xml files:

    limagito file mover filename filter

    • We’ll need a Pascal Script to convert the xml file to a Json file so we added Pascal Script as Destination:

    limagito file mover destination setup

    • We noticed that the xml we received from the customer was not a valid xml so we added some extra code (line 18) in the script to adjust this

    limagito filemover xml to json

    • Pascal Script we used (don’t forget to adjust the ctOutputPath const):
    Var
     tmpFileInput, tmpFileOutput, tmpResult: String;
     tmpXmlText: String;
     tmpList: TStringList;
    Const
      ctOutputPath = 'C:\Test\Out_JSon\'; // Must end with a \ 
    Begin
      // Init Var
      tmpFileInput := psFilePath + psFileName;
      tmpFileOutput := ctOutputPath + ChangeFileExt(psFileName, '.Json');
      psExitCode:= 0;
      // Read non valid Xml file and adjust before creating Json File
      tmpList := TStringList.Create;
      Try
        Try
          tmpList.LoadFromFile(tmpFileInput);
          tmpXmlText := tmpList.Text;
          tmpXmlText := psStringReplace(tmpXmlText, '-<', '<'); // Adjust Non Valid Xml
          // psLogWrite(1, '', 'StringReplace Xml Result: ' + tmpXmlText); // Debug
          tmpResult := psXmlTextToJSonFile(tmpXmlText, tmpFileOutput, True, True, True, True);
          If tmpResult = '' Then
          Begin
            psLogWrite(1, '', 'Saved Json Data to ' + tmpFileOutput);      
            psExitCode := 1;
          End  
          Else
            psLogWrite(1, '', tmpResult);
        Except
          psLogWrite(1, '', 'Load Xml from ' + tmpFileInput + ' Exception');
        End;
      Finally
        tmpList.Free;
      End;
    End.

    limagito file mover convert from xml to json

    In the example above we used The following function:

    Function psXmlTextToJSONFile(Const aXmlText, aJsonFile: String; Const aXmlParse, aXmlPack, aJsonIndent, aJsonEscape: Boolean): String;

    The psXML functions have a aXmlPack option that determines the format returned, Pack = False returns all tags as values with multiple nested levels of objects, Pack = True is simpler and easier to process but does lose some tag names.

    aXmlPack = False : all XML content is converted into Json values, each level with two or three names: #attributes is an optional object from opening tag, #Name
    is the string from <Tag>, and #children which is an array of other Json objects and arrays nested within the tag.

    aXmlPack = True : tags are converted to Names and their content to Values, or nested objects. For a single record, a single object is returned with the tag names
    lost, for multiple records (ie tags of the same name) an array is created with each record being an object.

     

    • RunTime Log Result:

    limagito file mover xml to json runtime log result

    • Json File Result:

     

    limagito filemover xml to json

    • Another available Pascal Script function that can be used only when source xml is valid (direct file to file conversion):

    Function psXmlFileToJSONFile(Const aXmlFile, aJsonFile: String; Const aXmlPack, aJsonIndent, aJsonEscape: Boolean): String;

    • Pascal Script will result in:
    Var
     tmpFileInput, tmpFileOutput, tmpResult: String;
    Const
      ctOutputPath = 'C:\Test\Out_JSon\'; // Must end with a \ 
    Begin
      // Init Var
      tmpFileInput := psFilePath + psFileName;
      tmpFileOutput := ctOutputPath + ChangeFileExt(psFileName, '.Json');
      psExitCode:= 0;
      // Xml File To JSon File
      tmpResult := psXmlFileToJSonFile(tmpFileInput, tmpFileOutput, True, True, True);
      If tmpResult = '' Then
      Begin
        psLogWrite(1, '', 'Saved Json Data to ' + tmpFileOutput);  
        psExitCode := 1;
      End  
      Else
        psLogWrite(1, '', tmpResult);
    End.
    

    limagito file mover convert from xml to json

    #filetransfer #mft #filemanagement #xml #json

    If you need any info  about this ‘Convert from XML to JSON format’ request, please let us know.

    Best regards,

    Limagito Team

  • 26 May

    Image and sidecar xml workflow question

    Q: We have a new process that backs up and then moves an image file and sidecar xml file into an input directory [folder A] for our automated photo toning software.
    The xml file contains instructions for cropping and has the same name as the image file with the exception of their file extensions, for example 230519_511627666.xml and 230519_511627666.jpg.
    The photo toning software ingests both the .xml and .jpg and attempts to tone the image, but if the .jpg fails it is output to an error folder [B] without being cropped and toned.
    We pick up the errored .jpg from folder B, resize it with other software and drop it to another folder [C].
    We need to then move the .jpg from folder C back to input folder A to try it again, but it requires its corresponding .xml file which can be found in the backup folder [D] be dropped alongside it again to process and tone.
    Is it possible with a Limagito rule to monitor source folder C and if an image file is found, grab the .xml file of the same name from folder D, then drop them both to folder A?
    Hope that makes sense! Thanks, David

    A: Yes, should be possible. We’ll prepare a script for this asap.

    • We created the following directory structure for the test of the script:

    Image and sidecar xml

    • Source is the C folder (resized files):

    Limagito file mover windows as source

    • Filename Include Filter, we only want to scan for jpg files:

    limagito file mover filename include filter

    • Enable and add ‘On Destination’ Pascal Script:

    limagito file mover pascal script

    • Don’t forget to adjust the Path const values:
    var
      tmpFilename: String;
    Const
      // Path must end with a \
      ctBackupPath = 'C:\Test\AJC\D\';
      ctDestinationPath = 'C:\Test\AJC\A\';
    Begin
      psExitCode:= 0;
      tmpFilename := ChangeFileExt(psFileName, '.xml');
      If FileExists(ctBackupPath + tmpFilename) Then
      Begin
        If psCopyFile(ctBackupPath + tmpFilename, ctDestinationPath + tmpFilename, False) Then
        Begin
           psExitCode:= 1;
           psLogWrite(1, '', 'Copy file success of ' + ctBackupPath + tmpFilename + ' to ' + ctDestinationPath + tmpFilename);
        End
        Else
        Begin
          psLogWrite(1, '', 'Copy file error of ' + ctBackupPath + tmpFilename + ' to ' + ctDestinationPath + tmpFilename);
        End;
      End
      Else
        psLogWrite(1, '', 'Error, xml file does not exist: ' + ctBackupPath + tmpFilename);
    End.

    limagito file mover pascal script as destination

    Feedback user:

    • Thank you, I’ll try this out and let you know if I have any issues with it. Appreciate the quick response!
    • Just wanted to update, this rule and script appears to work great and I have it in place in our production workflow now, thanks again!

    #filetransfer #mft #filemanagement

    If you need any info  about this ‘Image and sidecar xml workflow question’, please let us know.

    Best regards,

    Limagito Team

     

    By Limagito-Team Image Pascal Script , ,
  • 11 Jul

    Generate an xml on output with the same name as the input filename

    Q: I was wondering if Limagito File Mover can generate an xml on output with the same name as the input filename.

    A: Yes this is possible. In this case we’ll add a second Destination. This ‘Pascal Script’ Destination will create the Xml file. It will only generate this xml if the first Destination succeeds.

    1.You can use any Source. In our example we used a Windows Folder.

    2.We only want write the Xml file if the first Destination Succeeds. Therefore we have to select ‘Destination Memory & Exit Cyclus On Error’ as Destination Option in the Function Setup.

    Limagito File Mover Function Setup

    3. Add Pascal Script (PScript) as Second Destination

    Limagito File Mover Pascal Script as Destination

    4. Pascal Script Destination Setup

    We received the following example xml from the user (but we can generate other xml structures too):

    <?xml version=”1.0″ encoding=”iso-8859-1″?>
    <AdWatchData>
    <adwatch-ad-number>HOU4001521801</adwatch-ad-number>
    <file_name>HOU4001521801.pdf</file_name>
    </AdWatchData>

    We created the following Pascal Script which will generate the xml file. Please don’t forget to adjust the ‘ctOutputPath’ Const which will be different in your case. In our example we’ll create the xml file in: ‘C:\Test\Out\’.

    Limagito File Mover generate xml on output

    Var
      tmpFileExt: String;
      tmpFileName, tmpFileNameNoExt: String;
      tmpXml: TStringList;
    Const
      ctOutputPath = 'C:\Test\Out\'; // must end with a \
    Begin
      psExitCode:= 0;
      tmpFileExt := ExtractFileExt(psFileName);
      tmpFilenameNoExt := psStringReplace(psFileName, tmpFileExt, '');
      tmpFileName := tmpFileNameNoExt + '.xml';
      // Create XML
      tmpXml := TStringList.Create;
      Try
        tmpXml.Add('<!--?xml version="1.0" encoding="iso-8859-1"?-->')
        tmpXml.Add('');
        tmpXml.Add('' + tmpFileNameNoExt + '');
        tmpXml.Add('' + psFileName + '');
        tmpXml.Add(''); 
        // Save to File  
        Try
          tmpXml.SaveToFile(ctOutputPath + tmpFileName);
          psExitCode := 1;   
        Except
          psLogWrite(1, '', 'SaveToFile Error, File: ' + ctOutputPath + tmpFileName);
        End;
      Finally
        tmpXml.Free;
      End;  
    End.

    5.RunTimeLog Result:

    Limagito File Mover RunTime Log

    6. Generated Xml on Output:

    Limagito File Mover generate xml on output

    #FileTransfer

    If you need any info about this new ‘Generate an xml on output’ option, please let us know.

    Best regards,

    Limagito Team

    By Limagito-Team XML ,
1 2
SEARCH