Insert "strings" in XML code

Hello,

As a result of many operations, i get the below XML:

i need to insert the below line and close it off at the end…

blas blah blah…

image

should looks similar to this:

How can achieve this?

just adding it as a root attribute does not work…

Thank you in advance

DD

Hi @xewonder, can you share the JSON output of your final Function node as well as the line you’d like to insert as text rather than a screenshot? I am not super familiar with XML but am happy to give it a try on my side :slight_smile:

thank you so much

Blockquote
[
{
“”: “<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n\n \n \n weqrewrewqr\n GR\n 0\n \n \n 9982\n GR\n 0\n Som.\n \n \n \n Μάρκου \n \n 11\n Αθήνα\n \n \n \n RNT\n 1587\n GR\n \n \n \n \n 1\n 65.00000000\n 0\n 0\n \n icls:classificationtypeok</icls:classificationtype>\n icls:classificationCategory/\n \n icls:amount/\n icls:id/\n \n \n \n \n \n 2\n 2.00000000\n 0\n 0\n \n icls:classificationtypeok</icls:classificationtype>\n icls:classificationCategory/\n \n icls:amount/\n icls:id/\n \n \n \n”
}
]

this has to go between “?xml version” and “invoice”

(sorry… can’t seem to be able to paste the code)

should look like this:

“InvoicesDoc” should then be closed at the very end…

Sorry… just found out how to insert the text…

output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<invoice>
  <issuer>
    <vatNumber>weqrewrewqr</vatNumber>
    <country>GR</country>
    <branch>0</branch>
  </issuer>
  <counterpart>
    <vatNumber>99xxxxxxx</vatNumber>
    <country>GR</country>
    <branch>0</branch>
    <name>Soxxxxxx.</name>
    <address>
      <street>Μάρκοxxxxxxx</street>
      <number/>
      <postalcode>11xxxxx</postalcode>
      <city>Αθήνα</city>
    </address>
  </counterpart>
  <invoiceHeader>
    <series>RNT</series>
    <aa>1587</aa>
    <test>GR</test>
  </invoiceHeader>
  <invoiceDetails>
    <lineNumber>1</lineNumber>
    <netValue>65.00000000</netValue>
    <vatCategory>0</vatCategory>
    <vatAmount>0</vatAmount>
    <incomeClassification>
      <icls:classificationtype>ok</icls:classificationtype>
      <icls:classificationCategory/>
      <propertyName/>
      <icls:amount/>
      <icls:id/>
    </incomeClassification>
  </invoiceDetails>
  <invoiceDetails>
    <lineNumber>2</lineNumber>
    <netValue>2.00000000</netValue>
    <vatCategory>0</vatCategory>
    <vatAmount>0</vatAmount>
    <incomeClassification>
      <icls:classificationtype>ok</icls:classificationtype>
      <icls:classificationCategory/>
      <propertyName/>
      <icls:amount/>
      <icls:id/>
    </incomeClassification>
  </invoiceDetails>
</invoice>

the part to be inserted…

<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" xsi:schemaLocation="http://www.aade.gr/myDATA/invoice/v1.0 schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ic="https://www.aade.gr/myDATA/incomeClassificaton/v1.0">

how it should look at the end:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" xsi:schemaLocation="http://www.aade.gr/myDATA/invoice/v1.0 schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ic="https://www.aade.gr/myDATA/incomeClassificaton/v1.0">
<invoice>
  <issuer>
    <vatNumber>weqrewrewqr</vatNumber>
    <country>GR</country>
    <branch>0</branch>
  </issuer>
  <counterpart>
    <vatNumber>998xxxxx</vatNumber>
    <country>GR</country>
    <branch>0</branch>
    <name>Soxxxxxx.</name>
    <address>
      <street>Μάρκοxxxxx</street>
      <number/>
      <postalcode>11xxxx</postalcode>
      <city>Αθήνα</city>
    </address>
  </counterpart>
  <invoiceHeader>
    <series>RNT</series>
    <aa>1587</aa>
    <test>GR</test>
  </invoiceHeader>
  <invoiceDetails>
    <lineNumber>1</lineNumber>
    <netValue>65.00000000</netValue>
    <vatCategory>0</vatCategory>
    <vatAmount>0</vatAmount>
    <incomeClassification>
      <icls:classificationtype>ok</icls:classificationtype>
      <icls:classificationCategory/>
      <propertyName/>
      <icls:amount/>
      <icls:id/>
    </incomeClassification>
  </invoiceDetails>
  <invoiceDetails>
    <lineNumber>2</lineNumber>
    <netValue>2.00000000</netValue>
    <vatCategory>0</vatCategory>
    <vatAmount>0</vatAmount>
    <incomeClassification>
      <icls:classificationtype>ok</icls:classificationtype>
      <icls:classificationCategory/>
      <propertyName/>
      <icls:amount/>
      <icls:id/>
    </incomeClassification>
  </invoiceDetails>
</invoice>
</InvoicesDoc>
1 Like

Hi @xewonder, if this line will always come after the first line in your XML String, the below approach should work:

You can simply copy & paste the Insert Line node from the workflow above into your n8n canvas by first selecting it, then using Ctrl+C (to copy) and Ctrl+V (to paste).

This is the result:

This node is a Function Item node running custom JavaScript (as I couldn’t find a suitable built-in transformation in the XML node itself). It uses three different methods to achieve this:

  1. .split() to split up the XML string into individual lines
  2. .splice() to add a new line after the first one
  3. .join() to convert our array of lines back into a single string

Hope this helps!

thank you very much!

however, i get an error?

Try the Function Item node instead of the Function node.

ok…
I had to use a “set” and assign “data” to it

image

now i have another problem…

“data” is causing me a problem when trying to send the HTTP POST request.

how can i fix that?

Thank you

DD

Hey @xewonder,

Can you share the XML? The error seems to suggest it is not valid.

it’s kind of strange as I am only sending the XML?

there is no “data” there…

looks ok to me…

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<InvoicesDoc xmlns="http://www.aade.gr/myDATA/invoice/v1.0" xsi:schemaLocation="http://www.aade.gr/myDATA/invoice/v1.0 schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ic="https://www.aade.gr/myDATA/incomeClassificaton/v1.0">
<invoice>
  <issuer>
    <vatNumber>weqrewrewqr</vatNumber>
    <country>GR</country>
    <branch>0</branch>
  </issuer>
  <counterpart>
    <vatNumber>9xxxxxx</vatNumber>
    <country>GR</country>
    <branch>0</branch>
    <name>Soxxxxxxx</name>
    <address>
      <street>Μάxxxxxxx</street>
      <number/>
      <postalcode>11xxxxxx</postalcode>
      <city>Αθήνα</city>
    </address>
  </counterpart>
  <invoiceHeader>
    <series>RNT</series>
    <aa>1587</aa>
    <test>GR</test>
  </invoiceHeader>
  <invoiceDetails>
    <lineNumber>1</lineNumber>
    <netValue>65.00000000</netValue>
    <vatCategory>0</vatCategory>
    <vatAmount>0</vatAmount>
    <incomeClassification>
      <icls:classificationtype>ok</icls:classificationtype>
      <icls:classificationCategory/>
      <propertyName/>
      <icls:amount/>
      <icls:id/>
    </incomeClassification>
  </invoiceDetails>
  <invoiceDetails>
    <lineNumber>2</lineNumber>
    <netValue>2.00000000</netValue>
    <vatCategory>0</vatCategory>
    <vatAmount>0</vatAmount>
    <incomeClassification>
      <icls:classificationtype>ok</icls:classificationtype>
      <icls:classificationCategory/>
      <propertyName/>
      <icls:amount/>
      <icls:id/>
    </incomeClassification>
  </invoiceDetails>
</invoice>
</InvoicesDoc>

sorry…

let me check with proper settings for my post request

Hi @MutedJam !

With the new code node, there is an error:

Could you tell me what to do to fix it?

My problem was solved here:

I guess this is from this solved topic?

2 Likes

Hi @BramKn ! Yes, you’re right. I was trying to solve the problem.

1 Like

@Mikhail make sure to stick to one topic. If Tom didn’t see the other topic he could have spent time to try and help you, where it was already solved.

1 Like

ОК. I edited my post above and added a link

2 Likes

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