Reading Serial XML data and using the WRITE FILE NODE

Hello, as you will perceive I am new to Node-Red. I have a sensor that outputs serial data every minute in an XML format. I use a Serial IN node to capture the data. I send the XML data to a file using the write file node so that I could eventually (after learning more on Node-Red) read the file, transform to JASON, parse the data etc. and even send it to a DB.

When the serial node first sends its block of data to the write node the data is saved correctly if I use the APPEND TO FILE Action. If I use Overwrite file only the first line of the XML file is saved.

I want to capture the entire block of data and over write the data next time a new data set is retrieved. Here lies my problem. I was able to resolve it by copying the incoming data to a new file and erasing the old file but this is not elegant and I don’t understand why this step is causing me so much grief using the WRITE FILE NODE.

Any help is appreciated and thank you in advance.

Hi, welcome to the forum.
Can you share a reduced flow which demonstrates your problem?

Not sure I understand why you pass your data through a file, rather than parse & process the flow message all the way to the DB, or whatever final target. You have nodes which can convert your XML to JSON, CSV etc., or any proprietary format.

Further to omrid coment, there is always the node-red-contrib-fastxml (node) - Node-RED, which may offer a cleaner javascript object.

1 Like

Thank you for the suggestions. I will look at your suggestion and repost the question after I look into these options.

I have been able to send data to the Table but I cannot seem to be able to get all incoming data to display in one row. Instead it places some in a column and in its own row. I have tried removing \n from the serial node but then no data steam in. I believe the incoming data stream is not formatted correctly so only partial data is handled by the serial node and XML node. Here is a partial list of the errors.

I am placing a sample of the XML incoming serial table output and the xml serial feed to see if anyone has any ideas on how to send just one row of data to the table versus each data having its own row. I have a Serial node the XML Node the Table Node. Many thanks!!

The data output from the table and xml file are not exactly the same as the interval changed when I was capturing the screen shot. But its clear that only 5 variables are captured while I should have at least 7

This output is not xml, it is a string. First you need to get the xml from the data so that it becomes parsable.

You can use a function node, for example:

function extractXml(str) {
    // Find where the XML starts and ends
    const start = str.indexOf('<?xml');
    const end = str.lastIndexOf('>') + 1;
    if (start === -1 || end === -1) return null;
    return str.substring(start, end);
}

const input = msg.payload
const output = extractXml(input)

msg.payload = output

return msg;

You can pass the serial data through the function node and in turn pass that to an xml node.

(I think end will never be -1… if not found it would be 0 because of the +1)