Add data to XML file

Hello. I’m very new to n8n and I’m trying to simply add some data to an XML file. I know I have to read the file, convert it to JSON, edit data and then convert it back to XML. No problem with that, point is I have to write data in a specific part of the file, under a specific tag.

Here my input file :

<PROGETTI>
<PROGETTO NOME="PROG1">
	<VIDEO>
		<MENU>1</MENU>
		<TITOLO>History</TITOLO>
		<URL>https://www.youtube.com/1</URL>
	</VIDEO>
	<VIDEO>
		<MENU>2</MENU>
		<TITOLO>History</TITOLO>
		<URL>2</URL>
	</VIDEO>
</PROGETTO>
<PROGETTO NOME="PROG2">
	<VIDEO>
		<CLIENTE>CL1</CLIENTE>
		<DATA>24/08/2025</DATA>
		<TITOLO>Video 1</TITOLO>
		<URL>https://share.zight.com/1</URL>
	</VIDEO>
	<VIDEO>
		<CLIENTE>CL2</CLIENTE>
		<DATA>19/08/2025</DATA>
		<TITOLO>Video 2</TITOLO>
		<URL>https://share.zight.com/2</URL>
	</VIDEO>
	<VIDEO>
</PROGETTO>
</PROGETTI>

I have to add a simple element like this for example, to the SECOND project (the two projects have different structures) :

{ VIDEO:

{ DATA: Date.now(),
CLIENTE: “CL3”,
URL: “URL3” }
};

I’m looking for an output like this :

<PROGETTI>
<PROGETTO NOME="PROG1">
	<VIDEO>
		<MENU>1</MENU>
		<TITOLO>History</TITOLO>
		<URL>https://www.youtube.com/1</URL>
	</VIDEO>
	<VIDEO>
		<MENU>2</MENU>
		<TITOLO>History</TITOLO>
		<URL>2</URL>
	</VIDEO>
</PROGETTO>
<PROGETTO NOME="PROG2">
	<VIDEO>
		<CLIENTE>CL1</CLIENTE>
		<DATA>24/08/2025</DATA>
		<TITOLO>Video 1</TITOLO>
		<URL>https://share.zight.com/1</URL>
	</VIDEO>
	<VIDEO>
		<CLIENTE>CL2</CLIENTE>
		<DATA>19/08/2025</DATA>
		<TITOLO>Video 2</TITOLO>
		<URL>https://share.zight.com/2</URL>
	</VIDEO>
	<VIDEO>
		<CLIENTE>CL3</CLIENTE>
		<DATA>19/08/2025</DATA>
		<TITOLO></TITOLO>
		<URL>URL3</URL>
	</VIDEO>
	
</PROGETTO>
</PROGETTI>

I cannot make n8n to write data on the right position, just to appen data at the file end. Here the code I’ve use on my CODE node to achieve it (To JSON is my input file):

let jsonData = $(“To JSON”).all() // input data

// New record to add
const nuovoRecord = {

VIDEO:

{
DATA: Date.now(),
CLIENTE: “CL3”,
URL: “URL3”

}
};

// Add new record
jsonData.push(nuovoRecord);

// Return Json output
return jsonData.map(item => ({ json: item }));

Obviously, this way data are appended, but cannot make n8n to do something like jsonData.TagsthatIwant.push(nuovoRecord) without getting error.

Any smarter (and working) way to solve this? Any way without JavaScript?

Thank you so much in advance

Hey @datex hope all is good. Welcome to the community.

The input file looks rather … empty?..

Sorry, ok now, HTML section of the post doesn’t like XML apparently :slight_smile:

Here is one way to go about this:

(you may need to allow external modules for this)

by the way the original XML is invalid:

The line marked with a red X makes it invalid.

Another way to go about it:

1 Like

Sorry, my bad on copy and paste

Thank you, but what if values for the 4 new fields are variables, coming from a different node?

Something like this?

1 Like

Hello, I just found an anomaly. The XML output file structure is not exactly as the original one .. there is an addional tag “data” at the beginning and end of the file. Any clue? Thank you

This is probably because you saved it to a file in a way that preserved the data field. Show me how use used my example and where the data tag appears.

You may just run your example, it happens there too. It is added during the flow, it’s the name of the data field.

Ahh i see, here is the replacement code node:

use it instead the one in the workflow from before.

2 Likes

You’re a star!

Make sure to mark my answer as Solution, please and thank you!

Cheers!

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