XML Export

Share business templates, ideas, experiences, etc with fellow Servoy developers here

XML Export

Postby patrick » Thu Jan 27, 2005 3:12 pm

Hello all,

I have made a method that will create an XML file with data of any form containing

0. Creation information (when, who)
1. Table and column information (each table itself plus all columns in an extra node)
2. All records of a table

I have sort of "invented" my own grammar for this.

Open issues:

- eliminate characters that are not allowed in XML (like <)
- parse the format backwards (Import)
- split method in two to process more than one form in one file

If you find that useful, here is the code:

Code: Select all
// Typical call: generateXML([form], [descriptive], [xmlPath]);

// Declare variables

var xml = '',            // resulting XML
   form,                // the form containing the data to export
   table,                // the underlaying table of that form
   jsTable,            // jsTable of table   
   record,               // a record of that table
   columnArray,         // Array containing all column names of table
   jsColumn,            // a single jsColumn of that table
   fieldContent,         // a property of record representing its data
   currColumn,            // the name of a single column being processed
   pk,                  // the name of the primary key column of a table
   descriptive,         // a property to allow more descriptive XML
   xmlPath;            // the path to which a file can be written

// set variables; prepare

form = application.getMethodTriggerFormName(); // could also be arguments[0]
table = forms[form].controller.getTableName();
jsTable = databaseManager.getTable(forms[form].controller.getServerName(), table)
columnArray = jsTable.getColumnNames()
forms[form].controller.loadAllRecords();
descriptive = true; // could be arguments[1]
xmlPath = 'C:\test.xml'; // could be arguments[2]

// Header including <Created> info

xml += '<?xml version="1.0" encoding="ISO-8859-1" ?>'
xml += '<ServoyXMLexport version="1">'
xml += '<CREATED>'
xml += '<createdDate>' + application.getServerTimeStamp() + '</createdDate>'
xml += '<createdBy>' + security.getUserName() + '</createdBy>'
xml += '</CREATED>'

// TABLE: Table description -> the syntax could be used for more than one table in one XML file

if (descriptive)
{
   xml += '<TABLE name="' + table + '">'
}
else
{
   xml += '<TABLE>'
}
xml += '<Name>' + table + '</Name>'
xml += '<ServerName>' + jsTable.getServerName() + '</ServerName>'
xml += '<QuotedSQLName>' + jsTable.getQuotedSQLName() + '</QuotedSQLName>'
xml += '<SQLName>' + jsTable.getSQLName() + '</SQLName>'
xml += '<Form>' + 'abc' + '</Form>'
xml += '<TableID>' + '1234' + '</TableID>'

// TABLE/COLUMNS: column description of that table

xml += '<COLUMNS>'
for ( var i = 1 ; i <= columnArray.length ; i++ )
{
   jsColumn = jsTable.getColumn(columnArray[i-1])
   if (descriptive)
   {
      xml += '<COLUMN name="' + jsColumn.getSQLName() + '">'
   }
   else
   {
      xml += '<COLUMN>'
   }
   xml += '<SQLName>' + jsColumn.getSQLName() + '</SQLName>'
   xml += '<Type>' + jsColumn.getType() + '</Type>'
   xml += '<Length>' + jsColumn.getLength() + '</Length>'
   xml += '<QualifiedName>' + jsColumn.getQualifiedName() + '</QualifiedName>'
   xml += '<QuotedSQLName>' + jsColumn.getQuotedSQLName() + '</QuotedSQLName>'
   xml += '<AllowNull>' + jsColumn.getAllowNull() + '</AllowNull>'
   xml += '<isRowIdentifier>' + jsColumn.isRowIdentifier() + '</isRowIdentifier>'   
   if (jsColumn.isRowIdentifier())
   {
      pk = jsColumn.getSQLName().toLowerCase()
   }
   xml += '</COLUMN>'
}
xml += '</COLUMNS>'

// TABLE/RECORDS: the data of the table

xml += '<RECORDS>'
for ( var i = 1 ; i < forms[form].controller.getMaxRecordIndex() ; i++ )
{
   record = forms[form].foundset.getRecord(i)
   if (descriptive)
   {
      xml += '<RECORD ' + pk + '="' + record[pk] + '">'
   }
   else
   {
      xml += '<RECORD>'
   }
   for ( var j = 1 ; j <= columnArray.length ; j++ )
   {
      currColumn = columnArray[j-1].toLowerCase()
      fieldContent = record[currColumn]
      xml += '<' + currColumn + '>' + fieldContent + '</' + currColumn + '>'
   }
   xml += '</RECORD>'
}
xml += '</RECORDS>'

// Footer

xml += '</TABLE>'
xml += '</ServoyXMLexport>'

application.writeTXTFile(xmlPath, xml)
return xml;
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby bcusick » Fri Jan 28, 2005 3:48 am

Patrick,

EXCELLENT STUFF! Thanks for sharing it!
Bob Cusick
bcusick
 
Posts: 1255
Joined: Wed Apr 23, 2003 11:27 pm
Location: Thousand Oaks, CA USA

Postby patrick » Mon May 22, 2006 6:12 pm

And now there is also a plugin: http://forum.servoy.com/viewtopic.php?t=6431
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany


Return to Sharing Central

Who is online

Users browsing this forum: No registered users and 3 guests

cron