# Add data to XML file

**URL:** <https://community.n8n.io/t/add-data-to-xml-file/181424>\
**Category:** Questions\
**Tags:** xml\
**Created:** [Septembre 3, 2025, 2:30 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424 "2025-09-03T14:30:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 3, 2025, 2:30 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/1 "2025-09-03T14:30:18Z")

</div>

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 :

```auto
<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 :

```auto
<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

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 3, 2025, 2:41 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/2 "2025-09-03T14:41:37Z")

</div>

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

The input file looks rather … empty?..

---

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 3, 2025, 2:47 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/4 "2025-09-03T14:47:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 3, 2025, 3:01 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/5 "2025-09-03T15:01:02Z")

</div>

Here is one way to go about this:

(you may need to [allow external modules](https://docs.n8n.io/hosting/configuration/configuration-examples/modules-in-code-node/) for this)

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 3, 2025, 3:02 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/6 "2025-09-03T15:02:38Z")

</div>

by the way the original XML is invalid:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/b/9/b974dd91fe2c7e1d7e0d7ecedfaf939a647b2715.png)

The line marked with a red X makes it invalid.

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 3, 2025, 3:07 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/7 "2025-09-03T15:07:07Z")

</div>

Another way to go about it:

---

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 3, 2025, 3:50 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/8 "2025-09-03T15:50:50Z")

</div>

Sorry, my bad on copy and paste

---

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 3, 2025, 4:03 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/9 "2025-09-03T16:03:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 3, 2025, 4:18 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/10 "2025-09-03T16:18:21Z")

</div>

Something like this?

---

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 4, 2025, 4:18 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/11 "2025-09-04T16:18:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 4, 2025, 4:26 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/12 "2025-09-04T16:26:51Z")

</div>

> [@datex](#):
>
> The XML output file structure is not exactly as the original one

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.

---

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 4, 2025, 4:40 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/13 "2025-09-04T16:40:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 4, 2025, 4:54 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/14 "2025-09-04T16:54:15Z")

</div>

Ahh i see, here is the replacement code node:

use it instead the one in the workflow from before.

---

<div class="post-metadata">

**Author:** ![datex](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@datex](https://community.n8n.io/u/datex)\
**Post date:** [Septembre 4, 2025, 5:03 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/15 "2025-09-04T17:03:42Z")

</div>

You’re a star!

---

<div class="post-metadata">

**Author:** ![jabbson](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jabbson/32/87328_2.png) [@jabbson](https://community.n8n.io/u/jabbson)\
**Post date:** [Septembre 4, 2025, 5:11 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/16 "2025-09-04T17:11:41Z")

</div>

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

Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [Septembre 11, 2025, 5:12 UTC](https://community.n8n.io/t/add-data-to-xml-file/181424/17 "2025-09-11T17:12:19Z")

</div>

A fermé ce sujet (le Septembre 11, 2025, 5:12 UTC)

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