how to export an xml file

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

how to export an xml file

Postby nromeou » Mon Aug 02, 2010 9:49 pm

Hi,
i want to make an export into an xml file of some fields in a form, i tryied using the function printXML() but it doesnt do what i want that exports the entire form and i want to select only some of the record's fields. How can i do that? is there a servoy functionality for this or should i use a plugin?

Thanks in advance.
nromeou
 
Posts: 215
Joined: Fri Sep 18, 2009 8:38 pm
Location: Montevideo, Uruguay

Re: how to export an xml file

Postby Roberto Blasco » Tue Aug 03, 2010 10:24 am

Hi nromeou

If you don't want to use a plugin, do it directly. Xml is just a plain text tag formatted. Loop throw the foundset fulling a variable with the xml nodes and finally write the file with the variable content.

Code: Select all
function doXml()
{
   var xml = "<?xml version=\"1.0\" encoding=\"iso-18859-1\"?>" + "\n";
   xml += "<mi_xml>" + "\n";
   
   var num_reg = databaseManager.getFoundSetCount(foundset);
   for (var x=0; x<num_reg;x++){
      
      xml += "<articulo>" + "\n";
      xml += "<id>" + id_aplicacion + "</id>" + "\n";
      xml += "<nombre>" + nombre + "</nombre>" + "\n";
      xml += "</articulo>" + "\n";
   }
   xml += "</mi_xml>";
}


Best regards. Roberto.
Un saludo. Roberto.

Madrid - Spain
Tfno: (+34) 625653066
E-mail: roberto.blasco.serrano@gmail.com
User avatar
Roberto Blasco
007
 
Posts: 355
Joined: Tue Apr 08, 2008 7:18 pm
Location: Madrid / Spain

Re: how to export an xml file

Postby michel » Tue Aug 03, 2010 11:15 am

Why not use JSLib->XML? Then you always have nicely formed xml and you don't have to worry about quotes etc.
For instance something like:

Code: Select all
function doXml(fs)
{
  var xml = <my_xml/>
  var table_name = databaseManager.getDataSourceTableName(fs.getDataSource())
  var table = <my_table name={table_name}/>
  for(var i=1; i<=databaseManager.getFoundSetCount(fs); i++) {
    fs.setSelectedIndex(i)
    var record = <my_record index={i}/>
    var fields = databaseManager.getTable(fs).getColumnNames()
    for (var j=0; j<fields.length; j++) {
      var name = fields[j]
      var value = fs[name]
      var field = <my_field name={name}>{value}</my_field>
      record.appendChild(field)
    }
    table.appendChild(record)
  }
  xml.appendChild(table)
  return xml
}
Michel van Klink
Vision Development
michel
 
Posts: 186
Joined: Mon Sep 22, 2008 10:53 am
Location: Boskoop, The Netherlands

Re: how to export an xml file

Postby nromeou » Tue Aug 03, 2010 11:25 pm

thanks guys. problem solved with roberto´s code and adding this line of code after the for sentence

Code: Select all
for (var x=1; x<=num_reg;x++){
     foundset.setSelectedIndexes(x)
     xml += "<articulo>" + "\n";
nromeou
 
Posts: 215
Joined: Fri Sep 18, 2009 8:38 pm
Location: Montevideo, Uruguay


Return to How To

Who is online

Users browsing this forum: No registered users and 11 guests

cron