160 likes | 242 Views
A Generic SaveAs(XML). Paul Donohue. May 8th 2002 Hotel Senator Zürich. SaveAs(XML). The Desired Output. <?xml version='1.0'?> < table_name > < table_name _row> < col_name > Display Value </column_name> < col_name code=' Data ' > Display </col_name> </ table_name _row>
E N D
A Generic SaveAs(XML) Paul Donohue May 8th 2002 Hotel Senator Zürich
SaveAs(XML) The Desired Output <?xml version='1.0'?> <table_name> <table_name_row> <col_name>Display Value</column_name> <col_name code='Data' >Display</col_name> </table_name_row> </table_name>
SaveAs(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
SaveAs(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
SaveAs(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 = "<?xml version='1.0'?>" ls_xml_doc = ls_add_to_xml_doc
SaveAs(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
SaveAs(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)
SaveAs(XML) Get The Column Details • Find the column’s name, data type & edit style dw.Describe("#" + String(col_idx) + ".Name") dw.Describe("#" + String(col_idx) + ".ColType") dw.Describe("#" + String(col_idx) + ".Edit.Style")
SaveAs(XML) Get The Data • Truncate the first 5 characters of the data type • Call the appropriate GetItem function
SaveAs(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) ls_describe = ls_col_name + ".EditMask.Mask" ls_edit_mask = adw_save_me.Describe(ls_describe)
SaveAs(XML) Adding An Element • Column data is added as an element • dddw, ddlb & radio buttons also have an attribute • Any markup codes are replaced
SaveAs(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)) > 0 start += chunk stop += chunk LOOP
SaveAs(XML) Other Information • Concatenating strings is slow • BLOBs will improve performance • ERROR : End element was missing the character '> ' Caused by FileWrite truncating data • ERROR : Invalid at the top level of the document Caused by writing a BLOB to the file
PowerBuilder 9 XML Support in PB9 • XML Services • PBDOM – DOM in PowerScript • SaveAs(XML) • ImportFile(XML!)
Questions 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@pauldonohue.com Web : www.pauldonohue.com