In this example we’ll see how to export an invoice to an XML file. The script is an action in the invoice form.
Script Group
Form
Event
Action
documento = program.newxmldocument()
--create our empty xml document
nodoprincipale = documento.createelement("p","FatturaElettronica","http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2")
documento.appendchild("xml-stylesheet",'type="text/xsl" href="fatturapa_v1.2.xsl"')
attributo_nodo = documento.createattribute("xmlns:ds")
attributo_nodo.setvalue("http://www.w3.org/2000/09/xmldsig#")
nodoprincipale.appendattribute(attributo_nodo)
attributo_nodo1 = documento.createattribute("xsi","schemaLocation","http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 fatturaordinaria_v1.2.xsd")
nodoprincipale.appendattribute(attributo_nodo1)
attributo_nodo = documento.createattribute("xmlns:xsi")
attributo_nodo.setvalue("http://www.w3.org/2001/XMLSchema-instance")
nodoprincipale.appendattribute(attributo_nodo)
In this part of the script we’ve added the data required for the Italian electronic invoice (FatturaPA). It’s just an example showing how to nest nodes and assign attributes.
ProgressivoInvio = documento.createelement("ProgressivoInvio")
ProgressivoInvio.setvalue(dataview.getvalue("numero"))
nodoprincipale.appendchild(ProgressivoInvio)
We start creating the nodes we need and set their value from the form, in this case the invoice number. Remember that elements are always created from the document, but each node must then be “attached” to its parent.
tabellarighe = dataview.getatable("fattura_rows")
nrighe = tabellarighe.countrows()
righe = tabellarighe.getrows()
--get the rows that make up the invoice
DettaglioLinee = documento.createelement("DettaglioLinee")
for i = 1,nrighe do
valore_linea_codicearticolo = righe[i].getvalue("codice")
valore_linea_qta = utility.formatnum(righe[i].getvalue("qta"),5)
linea = documento.createelement("Linea")
linea_codicearticolo = documento.createelement("CodiceArticolo")
linea_codicearticolo.setvalue(valore_linea_codicearticolo)
linea_qta = documento.createelement("Qta")
linea_qta.setvalue(valore_linea_qta)
linea.appendchild(linea_codicearticolo)
linea.appendchild(linea_qta)
DettaglioLinee.appendchild(linea)
end
nodoprincipale.appendchild(DettaglioLinee)
We create a node called Linea that holds the data of each row, and add the code and quantity. This node is then attached to a parent node called DettaglioLinee, which in turn is attached to the main node.
documento.appendchild(nodoprincipale)
--sender data used for the file name (adapt to your own database)
paese_nomefile = "IT"
idfiscale_nomefile = "01234567890"
nomefile = paese_nomefile .. idfiscale_nomefile .. "_" .. ProgressivoInvio.getvalue()
cartella_fatture = utility.openfolderdialog()
if (cartella_fatture~="") then
if nomefile ~= "" then
nomefile = cartella_fatture .. "/" .. nomefile .. ".xml"
documento.save(nomefile)
dataview.showconfirm("Electronic invoice file created successfully")
end
end
Finally, we attach nodoprincipale to the document, set the file name and ask the user which folder to save it in.
As you can see, nodoprincipale was used throughout the script but only attached to the document at the end. For the script it makes no difference whether the node is attached at the start or at the end; what matters is that it happens before the XML file is actually saved.
