Search code examples
javascriptnode-red

How to detect end-of-file when using "file-in" and "xml" nodes?


I am using Node-RED to extract data from an XML file. I am using the file-in node to read the file line-by-line and feed the output to an xml node to convert it into a Javascript object and pass it to a function node.

enter image description here

Given below sample XML, I would like to trigger a condition in function node when </X12> is encountered.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<X12>
  <INTERCHANGE>
    <GROUP>
      <GS-FunctionalGroupHeader>
        <GS01-FunctionalIdentifierCode>GF</GS01-FunctionalIdentifierCode>
        <GS02-ApplicationSendersCode>CARRIERID</GS02-ApplicationSendersCode>
      </GS-FunctionalGroupHeader>
    </GROUP>
    <IEA-InterchangeControlTrailer>
      <IEA01-NumberOfIncludedFunctional>1</IEA01-NumberOfIncludedFunctional>
      <IEA02-InterchangeControlNumber>1504</IEA02-InterchangeControlNumber>
    </IEA-InterchangeControlTrailer>
  </INTERCHANGE>
</X12>

Solution

  • For the file-in node in a msg per line mode each msg object should include a msg.parts section. This includes msg.parts.index which will include the line number from the file.

    The msg for the last line should also include msg.parts.count which will hold the total number of lines in the file.

    The xml node expect a whole XML document and will convert that into an equivalent JSON object, it has no concept of end of file.