Title: A Generic SaveAsXML
1A Generic SaveAs(XML)
Paul Donohue
May 8th 2002 Hotel Senator Zürich
2SaveAs(XML)
The Desired Output
- lt?xml version'1.0'?gt
- lttable_namegt
- lttable_name_rowgt
- ltcol_namegtDisplay Valuelt/column_namegt
- ltcol_name code'Data' gtDisplaylt/col_namegt
- lt/table_name_rowgt
- lt/table_namegt
3SaveAs(XML)
Design Guidelines
- The function must be generic
- The function must cater for large datasets
- The function must handle special markup codes
- The function must cater for edit styles
4SaveAs(XML)
Function Overview
- Add the XML declaration
- Add the root element
- Loop through each row in the datawindow
- Loop through each column in the row
- Get the column data
- Format the data
- Add the data as an element
- Save the data to a text file
5SaveAs(XML)
The XML Declaration
- The first line of any XML file
- This is hard coded
- The XML is being built up as a string
ls_add_to_xml_doc "lt?xml version'1.0'?gt"
ls_xml_doc ls_add_to_xml_doc
6SaveAs(XML)
The Root Element
- The root element is the dw update table
- For a read-only dw the word datawindow is used
ls_describe "DataWindow.Table.UpdateTable" ls_dw
_table_name adw_save_me.Describe(ls_describe) IF
ls_dw_table_name "?" THEN ls_dw_table_name
"Datawindow" END IF
7SaveAs(XML)
The Loops
- There are 2 loops
- One for the rows and one for the columns
ll_max_rows adw_save_me.RowCount() ll_max_cols
Long(dw.Object.DataWindow.Column.Count)
8SaveAs(XML)
Get The Column Details
- Find the columns name, data type edit style
dw.Describe("" String(col_idx)
".Name") dw.Describe("" String(col_idx)
".ColType") dw.Describe("" String(col_idx)
".Edit.Style")
9SaveAs(XML)
Get The Data
- Truncate the first 5 characters of the data type
- Call the appropriate GetItem function
10SaveAs(XML)
Format The Data
- For dddw, ddlb radio buttons use the display
value - For edit masks apply the mask
ls_describe Evaluate('LookUpDisplay("
ls_col_name ")', " String(ll_row)
")" ls_display_value dw.Describe(ls_describe) l
s_describe ls_col_name ".EditMask.Mask" ls_edi
t_mask adw_save_me.Describe(ls_describe)
11SaveAs(XML)
Adding An Element
- Column data is added as an element
- dddw, ddlb radio buttons also have an attribute
- Any markup codes are replaced
12SaveAs(XML)
Saving The XML File
- FileWrite can write only 32766 bytes at a time
- Use a loop to write chunks of data
chunk 32765 start 1 stop chunk DO WHILE
FileWrite(file, Mid(xml, start, stop)) gt 0
start chunk stop chunk LOOP
13SaveAs(XML)
Other Information
- Concatenating strings is slow
- BLOBs will improve performance
- ERROR End element was missing the character 'gt
' Caused by FileWrite truncating data - ERROR Invalid at the top level of the document
Caused by writing a BLOB to the file
14Demonstration
15PowerBuilder 9
XML Support in PB9
- XML Services
- PBDOM DOM in PowerScript
- SaveAs(XML)
- ImportFile(XML!)
16Questions
- If you have any questions about this presentation
or you would like a copy of the PowerBuilder
objects please email me or visit my web site. - Email info_at_pauldonohue.com
- Web www.pauldonohue.com