XML

  • 05 Nov

    How to adjust text in a xml file

    How to adjust text in a xml file

    Q: How can I setup a watch folder for any xml files come in and search for specific wording and change it to another word and move it out to an output directory.

    For the attach example:

    • If you see the word “  <Main>1</Main>”  then change to <Main>Good</Main>
    • If you see other word beside “1” then chang to <Main>NoGood</Main>

    Example content:

    <?xml version=”1.0″?>
    <Header>
    <Main>1</Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

    A: Yes this is possible using ‘Pascal Script’ as Destination.

    Source is a Windows folder:

    limagito file mover windows folder as source

    We adjusted the ‘Include Filename Filter’ to *.xml because we only want to handle xml files here.

    limagito filemover include filename filter

    We added our ‘Pascal Script’ option as Destination:

    limagito file mover pascal script as destination

    We added the following script. Please do not forget to adjust the ctOutputPath const which must end with a \

    You can download the script: here

    limagito file mover adjust text in a xml file

    RunTime Log result:

    limagito file mover runtime log result

    Content of the 3 source test files we used:

    1. TEST.Good.xml

    <?xml version=”1.0″?>
    <Header>
    <Main>1</Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

    2. TEST.NoGood.xml

    <?xml version=”1.0″?>
    <Header>
    <Main>0</Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

    3. TEST.NoGood.blanc.xml

    <?xml version=”1.0″?>
    <Header>
    <Main></Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

    Content of the 3 resulting Destination files:

    1. TEST.Good.xml

    <?xml version=”1.0″?>
    <Header>
    <Main>Good</Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

    2. TEST.NoGood.xml

    <?xml version=”1.0″?>
    <Header>
    <Main>NoGood</Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

    3. TEST.NoGood.blanc.xml

    <?xml version=”1.0″?>
    <Header>
    <Main>NoGood</Main>
    <name>Geen</name>
    <Date>221023</Date>
    </Plan>

     

    #xml #filetransfer #filemanagement

    If you need any help with this ‘adjust text in a xml file’ request, please let us know.

    Best Regards,

    Limagito Team

  • 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 ,
  • 11 Nov

    Pascal Script Xml Functions added to Limagito File Mover

    Pascal Script Xml Functions added to Limagito File Mover

    We added two extra Pascal Script functions in version v2020.11.11.0.

    • Function psXMLGetAttribute(Const aXML, aRootNode, aChildNode, aAttrName: String): String;
    • Function psXMLSetAttribute(Const aXML, aRootNode, aChildNode, aAttrName, aAttrValue: String): String;

    These functions allow you to Get and Set Xml Attributes. aRootNode or aChildNode can be empty. We prepared the Pascal Script engine to be ready for other Xml requests users may have. Don’t hesitate to ask for extra Xml features.

    We added these functions because we received the following request:

    Currently I have different XML-Files which more or less will have static file contents besides that it would be needed to update one field with the current date of the day. Scheduler will be used and set to run every weekday at approx. 23:59:59.  In the files the XML-field document > date need to be updated in date format YYYY-MM-DD.

    Example for today (2020-11-11):

    <document level=”01-01-xx-xx-xx” date=”2020-11-11″ name=”davinci”>

    Rest of the file contents stays unchanged.

     

    • In the following example will change the value of a node attribute. The Source will be a Xml file (WIN as Source).

    Limagito File Mover WIN as Source

    • The Destination is Pascal Script. The Script will use these new function and write the adjusted Xml content to a new file.

    Limagito File Mover Pascal Script as Destination

    • Don’t forget to adjust the Const ctDest to the path you need the adjusted xml to be saved in.

    Var
      iList: Integer;
      tmpDateStr: String;
      tmpList: TStringList;
      tmpPos: Integer;
      tmpXml: String;
    Const
      ctDest = 'C:\Test\In\Patrick\Out\';
    Begin
      psExitCode:= 0;
      // ... add your code here
      // <!--?xml version="1.0" encoding="ISO-8859-1"?-->
      // 
      // 
      tmpList := TStringList.Create;
      Try
        Try
          // Load Xml File
          psLogWrite(1, '', 'Loading ' + psFilePath + psFileName);
          tmpList.LoadFromFile(psFilePath + psFileName);
          // Prepare Date as String
          tmpDateStr := FormatDateTime('YYYY-MM-DD', Now); // i.e. 2020-11-10
          // Function psXMLGetAttribute(Const aXML, aRootNode, aChildNode, aAttrName): String;
          tmpXml := psXMLGetAttribute(Trim(tmpList.Text), 'document', '', 'date');
          psLogWrite(1, '', 'XML Get Date Attr Value: ' + tmpXML);
          // Function psXMLSetAttribute(Const aXML, aRootNode, aChildNode, aAttrName, aAttrValue: String): String;
          psLogWrite(1, '', 'XML Set Date Attr Valuet ' + tmpDateStr);
          tmpXml := psXMLSetAttribute(Trim(tmpList.Text), 'document', '', 'date', tmpDateStr);
          // Save To File
          // psLogWrite(1, '', 'XML Result: ' + tmpXml);
          tmpList.Text := Trim(tmpXML);
          Try
            tmpList.SaveToFile(ctDest + psFileName);
            psLogWrite(1, '', 'SaveToFile: ' + psFilePath + psFileName + ' Successful');
            // Set Result
            psExitCode:= 1;
          Except
            psLogWrite(1, '', 'SaveToFile:' + psFilePath + psFileName + ' Error');
          End;
        Except
          psLogWrite(1, '', 'LoadFromFile: ' + psFilePath + psFileName + ' Error');
        End;
      Finally
        tmpList.Free;
      End;
    End.

     

    • Original Source Xml file contains a Date attribute with value “2018-11-30”:

    Limagito File Mover Xml Source example

    • The new Destination Xml file contains the adjusted Date attribute with value “2020-11-11”:

    Limagito File Mover Xml Destination Example

    • RunTime log result:

    Limagito File Mover Xml RunTime Log

    If you need any help with these Pascal Script ‘Xml functions’, please let us know.

    Best Regards,

    Limagito Team

    By Limagito-Team Pascal Script XML ,
SEARCH