Write-xml: Exporting A Single Dataset Record

Is there any way to export a single dataset record, either using WRITE-XML or some other method?

The code below is a simplified example of what I'm trying to do: Export each tt1 and associated tt2 data into individual files, each file with a single tt record and its associated td records. The code below is exporting the entire dataset three times. Can someone point me in the right direction?

Much appreciated,

-Dan
Code:
define variable   h          as handle.
define variable   vcFileName as character.
define temp-table tt  no-undo
  field t1 as char.
define temp-table td  no-undo
  field td1 as char
  field td2 as char.

define dataset dsTest for tt, td
  data-relation dsJoin for tt, td
  relation-fields (t1, td1).

assign h = buffer tt:handle.

create tt.
assign t1 = "a".
create td.
assign td1 = "a"
       td2 = "Test 1".
create tt.
assign t1 = "b".
create td.
assign td1 = "b"
       td2 = "Test 2".
create td.
assign td1 = "b"
       td2 = "Test 2b".
create tt.
assign t1 = "c".
create td.
assign td1 = "c"
       td2 = "Test 3".

for each tt:
  assign vcFileName = "/tmp/" + t1 + ".txt".
  dataset dsTest:write-xml("file", vcFileName, true).
end.
 
Last edited by a moderator:

GregTomkins

Active Member
You could WRITE-XML on the individual buffer handles buried inside tt1 and tt2 (via their table handles). But there is probably a more elegant way to do it by querying within the PDS.
 
Thanks, Greg. I tried that as well - but got stuck on exporting each record in the temp-table.

I tried something along the lines of the code below - the best I could do was to export the entire table contents into individual XML files, not just the contents of a specific record into an XML file. Can you tell me what I'm doing wrong?

-Dan

DEFINE TEMP-TABLE tt NO-UNDO
FIELD tt1 AS CHARACTER.

DEFINE VARIABLE vcFileName AS CHARACTER NO-UNDO.

CREATE tt.
ASSIGN tt1 = "a".

CREATE tt.
ASSIGN tt1 = "b".

FOR EACH tt:
ASSIGN vcFileName = "/tmp/" + vcFileName + ".xml".
BUFFER tt:WRITE-XML("file", VALUE(vcFileName), "TRUE"). /* This line doesn't work, I was experimenting with handles and buffers to no effect */
END.
 

Stefan

Well-Known Member
A bit of a hack job, a second set of temp-tables is defined which are attached as data sources to the dataset and then filled with an adjusted fill-where-string:

Code:
define temp-table tt  no-undo
  field t1 as char.
define temp-table ttds like tt.

define temp-table td  no-undo
  field td1 as char
  field td2 as char.
define temp-table tdds like td.

define dataset dsTest for ttds, tdds
  data-relation dsJoin for ttds, tdds
  relation-fields (t1, td1) nested foreign-key-hidden.

define data-source dstt for tt.
define data-source dstd for td.

buffer ttds:handle:attach-data-source( data-source dstt:handle ).
buffer tdds:handle:attach-data-source( data-source dstd:handle ).

create tt. tt.t1 = "a".
create td. assign td.td1 = "a" td.td2 = "Test 1".

create tt. tt.t1 = "b".
create td. assign td.td1 = "b" td.td2 = "Test 2".
create td. assign td.td1 = "b" td.td2 = "Test 2b".

create tt. tt.t1 = "c".
create td. assign td.td1 = "c"td.td2 = "Test 3".

def var lcc as longchar.

for each tt:
  data-source dstt:fill-where-string = substitute( "where tt.t1 = &1", quoter( tt.t1 ) ).
  dataset dsTest:fill().
  dataset dsTest:write-xml("longchar", lcc, true).
  dataset dsTest:empty-dataset().
  message string(lcc)view-as alert-box.
end.
 
Top