# Reading Serial XML data and using the WRITE FILE NODE

**URL:** https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777
**Category:** General
**Created:** [21 August 2025 21:35 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777 "2025-08-21T21:35:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![p9sterling](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@p9sterling](https://discourse.nodered.org/u/p9sterling)
#### Post date: [21 August 2025 21:35 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/1 "2025-08-21T21:35:40Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)
#### Post date: [21 August 2025 21:55 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/2 "2025-08-21T21:55:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [22 August 2025 06:41 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/3 "2025-08-22T06:41:54Z")

</div>

Further to omrid coment, there is always the [node-red-contrib-fastxml (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-fastxml), which may offer a cleaner javascript object.

---

<div class="post-metadata">

### Author: ![p9sterling](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@p9sterling](https://discourse.nodered.org/u/p9sterling)
#### Post date: [22 August 2025 22:04 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/4 "2025-08-22T22:04:02Z")

</div>

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

---

<div class="post-metadata">

### Author: ![p9sterling](https://avatars.discourse-cdn.com/v4/letter/p/9fc348/32.png) [@p9sterling](https://discourse.nodered.org/u/p9sterling)
#### Post date: [23 August 2025 02:50 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/5 "2025-08-23T02:50:42Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db46ea20e091ff6700cec5007a90e380377e99a8.png)

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!!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ebda85bc0836604862f91af5275048db323c4bf.png)

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/3/f31cd99dddb56c183bc5158562610d400a7e9ffb.png)

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [23 August 2025 04:27 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/6 "2025-08-23T04:27:03Z")

</div>

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:

```javascript
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.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [23 August 2025 07:56 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/7 "2025-08-23T07:56:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [21 November 2025 07:57 UTC](https://discourse.nodered.org/t/reading-serial-xml-data-and-using-the-write-file-node/98777/8 "2025-11-21T07:57:05Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
