Export in an XML file

In this example we will see how to export an invoice within an xml file. This script will be an action within the invoice form.

Script Group

Form

Event

Value is being modified


document = program.newxmldocument()

Let's create our empty xml document.

mainnode = document.createelement("p","FatturaElettronica","http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2")

document.appendchild("xml-stylesheet",'type="text/xsl" href="fatturapa_v1.2.xsl"')


attribute_node = document.createattribute("xmlns:ds")

attribute_node.setvalue("http://www.w3.org/2000/09/xmldsig#")

mainnode.appendattribute(attribute_node)

attribute_node1 = document.createattribute("xsi","schemaLocation","http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 fatturaordinaria_v1.2.xsd")

mainnode.appendattribute(attribute_node1)

attribute_node = document.createattribute("xmlns:xsi")

attribute_node.setvalue("http://www.w3.org/2001/XMLSchema-instance")

mainnode.appendattribute(attribute_node)

In this segment of the script we have entered the data required for italian electronic invoice. This is just an example to see how to nest nodes and assign attributes.

progessivesending = document.createelement("ProgressivoInvio")

progessivesending.setvalue(dataview.getvalue("numero"))

mainnode.appendchild(progessivesending)

We begin to create the nodes we need and set the value by retrieving it from the form which, in this case, is the number invoice. Remember that the creation of the elements always starts from the document, but then it is necessary to “hook” the node to the father.

table_rows = dataview.getatable("invoice_rows")

nrows = table_rows.countrows()

rows = table_rows.getrows()

Let's extrapolate the rows that make up the invoice.

DetailLines= document.createelement("DetailLines")


for i = 1,nrows do

value_line_itemcode = rows[i].getvalue("code")

value_line_qty = utility.formatnum(rows[i].getvalue("qty"),5)

utility.formatnum(rows[i].getvalue("qty"),5)

line = document.createelement("Line")

line_itemcode = document.createelement("ItemCode")

document.createelement("ItemCode")

line_itemcode.setvalue(value_line_itemcode)


line_qty = document.createelement("Qty")

line_qty.setvalue(value_line_itemcode)


line.appendchild(line_itemcode)

line.appendchild(line_qty)

detaillines.appendchild(line)

end

mainnode.appendchild(detaillines)

Let's create a node called Line which will contain the data of each single row and insert code and qty. This node will then be connected to a father node called detaillines which in turn will be connected to the main node.

document.appendchild(mainnode)

filename = country_filename .. idfiscal_filename .. "_" .. progessivesending.getvalue()

folder_invoices = utility.openfolderdialog()


if (folder_invoices~="") then

if filename ~= "" then

filename = folder_invoices .. "\\" .. filename .. ".xml"

document.save(filename)

dataview.showconfirm("File invoice electronics successfully created")

end

end

To conclude, we connect the mainnode to the document, set the file name and ask the user in which folder it should be saved.

As you can see, the mainnode was used for the whole script, but only at the end was it linked to the document. However, for the purposes of the script it does not change if the node is connected at the beginning or at the end, the important thing is that this operation is performed before the actual saving of the xml file.