Add "file" into XML

Posted by xjg on 13-Oct-2017 08:03


This may be a silly question but I was wondering if I could "add" a file within an XML file.

I'm working in a development where I need to "attach" some files to an existing XML file to be later transferred to another system.

I was thinking (and I've already done some steps) of creating some extra nodes in the XML file and just use the method 

MEMPTR-TO-NODE-VALUE( ) from the x-noderef class.

However, in the receiving system, I'm having some issues when splitting the file into the original part and those "attachments". 

At first, in the sending side I just did:

COPY-LOB FROM FILE 'myfile.pdf' TO mData.  where mData is a memptr.  Afterwards, I ran the memptr-to-node-value() and the try to split the file in the receiving side, but the file I was able to create seem corrupted and for sure had a smaller size..I couldn't open it.

In my second attempt I added an BASE64-ENCODE(mData) and the memptr I passed to the x-noderef was binary data in the sending side. However, when trying to split the file I get "Invalid character data found in MEMPTR for codepage ISO8859-1 (12012)".

Does someone know how I could fix this keeping in mind that my goal is just to send everything in this same XML file?

Thanks in advance!

All Replies

Posted by Brian K. Maher on 13-Oct-2017 08:06

base64 encode the data and add it to a cdata node

Posted by xjg on 13-Oct-2017 08:43

Hello Brian,

Thanks for your quick response.

I've done something as follows:

Sending side:

COPY-LOB FROM FILE 'myfile.pdf' TO mData.  (mData is a memptr)

lv_pdfFile = BASE64-ENCODE(mData).  (lv_pdfFile is a longchar)

COPY-LOB FROM lv_pdfFile TO mb64Data. (mb64Data is a memptr)


hxdoc:CREATE-NODE(hNoderefValue, "", "CDATA-SECTION").



Receiving side:


hNodeAttach_cont:NODE-VALUE-TO-MEMPTR(hNodeContent). (hNodeContent is a memptr)

COPY-LOB FROM hNodeContent to lv_pdfFile. (lv_pdfFile is memptr and lv_pdfFile is a longchar)

lv_mData = BASE64-DECODE(lv_pdfFile). (lv_mData is a memptr) 

COPY-LOB FROM lv_mData to FILE "myreceivedfile.pdf".

Still receiving the error "Invalid character data found in MEMPTR for codepage ISO8859-1" in the yellow-highlighted line

I've tried to use the CONVERT, NO-CONVERT expressions without any success.. 

Any idea?

Posted by Brian K. Maher on 13-Oct-2017 08:46

Can you send the actual code so I can look at it?

Posted by xjg on 13-Oct-2017 08:59

Sending side:

define variable lv_mData                      as memptr            no-undo.
define variable lv_mb64Data                as memptr            no-undo.
define variable lv_pdfFile                      as longchar          no-undo.
define variable iph_xdocument             as handle  no-undo.
define variable hNodeRef                   as handle no-undo.

define variable hNoderefChild           as handle no-undo.
define variable hNoderefChild2         as handle no-undo.
define variable hNoderefValue          as handle no-undo.

create x-noderef hNodeRef.
create x-noderef hNoderefChild.
create x-noderef hNoderefChild2.
create x-noderef hNoderefValue.

/* File to memptr */

COPY-LOB FROM FILE 'myFile.pdf' TO lv_mData.

lv_pdfFile = BASE64-ENCODE(lv_mData).

COPY-LOB FROM lv_pdfFile TO lv_mb64Data.

/* Create the structure


       <Name> file_name </name>

       <Content> FILE CONTENT HERE </content>



iph_xdocument:CREATE-NODE(hNoderefChild, "Attachment", "ELEMENT").


iph_xdocument:CREATE-NODE(hNoderefChild2, "Name", "ELEMENT").


iph_xdocument:CREATE-NODE(hNoderefValue, "", "TEXT").


hNoderefValue:NODE-VALUE = "myFile.pdf".

iph_xdocument:CREATE-NODE(hNoderefChild2, "Content", "ELEMENT").


iph_xdocument:CREATE-NODE(hNoderefValue, "", "CDATA-SECTION").



Receiving side:

  define input parameter iph_hNodeRef  as handle no-undo.

  define variable hNodeAttach          as handle    no-undo.

  define variable hNodeContent        as memptr    no-undo.

  define variable hNodeAttach_cont  as handle    no-undo.

  define variable lv_pdfFile        as longchar  no-undo.

  define variable lv_mData         as memptr    no-undo.

  create x-noderef hNodeAttach.

  create x-noderef hNodeAttach_cont.


        if (hNodeAttach:LOCAL-NAME) = "Content" then do:

           hNodeAttach:GET-CHILD(hNodeAttach_cont, 1).


           COPY-LOB FROM hNodeContent to lv_pdfFile.

           lv_mData = BASE64-DECODE(lv_pdfFile).

           COPY-LOB FROM lv_mData to FILE "myreceivedfile.pdf".


I've changed somethings cause the code is split in different procedures I tried to not miss any of the key parts.

Posted by xjg on 16-Oct-2017 09:29

Hi again, I've tried working with the SAX-writer / SAX-reader, but eventually I end up with the same "Invalid character data found in MEMPTR for codepage ISO8859-1" error.

At some point, because of the BASE64-ENCODE/DECODE and the usage of the copy-lob I have an issue with the codepage ..

Any suggestion?

Posted by Stefan Drissen on 16-Oct-2017 11:01

I'd say it's some kind of bug / really crap default handling on copy-lob from memptr to longchar. The memptr is null terminated, the null is screwing up the copy-lob.

If you change:

COPY-LOB FROM hNodeContent to lv_pdfFile.


COPY-LOB FROM hNodeContent starting at 1 for get-size( hnodecontent ) - 1 to lv_pdfFile.

Then you should be fine (it worked for me).

BTW next time, please add a snippet or a snippet that actually compiles. It lowers the barrier for anyone wanting to actually look at your issue.

This thread is closed