Add and read CDATA into and from a XML file

Potish

Member
I am working on generating an XML file for a SOAP API with a vendor. I am using pro-datasets to populate the data to be included in the XML file.

One of the fields in the request requires CDATA be sent similar to the following

<req:RequestMsg><![CDATA[<?xml version='1.0' encoding='UTF-8'?><request> <Transaction> <CommandID>SomeData</CommandID></Transaction> </request>]]></req:RequestMsg>

The response also has a CDATA section similar to the following
<ResultMsg><![CDATA[<Result><ResultType>Completed</ResultType></Result>]]></ResultMsg>

How can I
1. Add the request message XML into the pro-dataset /temp-table field. When I use a simple assign for the request I end up with something like this
<RequestMsg>&amp;lt;![CDATA[&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;request&gt; &lt;Transaction&gt; &lt;CommandID&gt;SomeData&lt;/CommandID&gt; &lt;/Transaction&gt; &lt;/request&gt; ]]&amp;gt;</RequestMsg>

2. Read the result message XML from the response
 

KrisM

Member
A simple example of adding CDATA into an xml document:

Code:
define variable h1 as handle.
define variable h2 as handle.
define variable h3 as handle.
define variable cdata as longchar.

cdata = "<?xml version='1.0' encoding='UTF-8'?><request> <Transaction> <CommandID>SomeData</CommandID></Transaction> </request>".

create x-document h1.
create x-noderef h2.
create x-noderef h3.
h1:create-node(h2, "RequestMap", "element").
h1:append-child(h2).
h1:create-node(h3, "", "cdata-section").
h2:append-child(h3).
h3:longchar-to-node-value(cdata).

Is this what you are looking for ?
 

Potish

Member
A simple example of adding CDATA into an xml document:

Code:
define variable h1 as handle.
define variable h2 as handle.
define variable h3 as handle.
define variable cdata as longchar.

cdata = "<?xml version='1.0' encoding='UTF-8'?><request> <Transaction> <CommandID>SomeData</CommandID></Transaction> </request>".

create x-document h1.
create x-noderef h2.
create x-noderef h3.
h1:create-node(h2, "RequestMap", "element").
h1:append-child(h2).
h1:create-node(h3, "", "cdata-section").
h2:append-child(h3).
h3:longchar-to-node-value(cdata).

Is this what you are looking for ?
Thank you for the suggestion.

I am currently using a prodataset and write-xml to create the XML. If I am not able to find a solution for the prodataset I will look to implement your suggested solution in place of the prodataset.

Here is a sample of the code I am using

define variable vlcxml as longchar no-undo.
define variable vlreturn as logical no-undo.
define variable vhDoc as handle no-undo.
define variable vhRoot as handle no-undo.


define temp-table ttBody xml-node-name "soapenv:Body"
field id as integer serialize-hidden.

define temp-table ttRequestMsg namespace-prefix "req"
xml-node-name "RequestMsg"
field id as integer serialize-hidden
field RequestMsg as character xml-node-type "text".


define dataset dsEnvelope xml-node-name "soap:Envelope"
for ttBody,
ttRequestMsg
data-relation r06 for ttBody, ttRequestMsg relation-fields (id, id) nested.

create ttBody.
assign
ttBody.id = 1.

create ttRequestMsg.
assign
ttRequestMsg.id = ttBody.id.

ttRequestMsg.RequestMsg = '<![CDATA[<Result><ResultType>Completed</ResultType></Result>]]>'.

vlreturn = dataset dsEnvelope:write-xml("longchar", /* file | stream | stream-handle | memptr | handle | longchar */
vlcxml,
yes, /* formatted */
"utf-8", /* encoding */
?, /* schema-location */
no, /* write-xmlschema */
no, /* min-xmlschema */
no, /* write-before-image */
yes). /* omit-initial-values */

create x-document vhDoc.
vhDoc:suppress-namespace-processing = true.
create x-noderef vhRoot.

vhDoc:load("longchar", vlcxml, false).

vhDoc:get-document-element(vhRoot).
vhRoot:set-attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/").
vhRoot:set-attribute("xmlns:req", "http://api-v1.gen.mm.vodafone.com/mminterface/request").
vhDoc:save("file", "sampleRequest.xml").
 

Potish

Member
Here is sample of code I am using to create the XML file with CDATA

define variable vlcxml as longchar no-undo.
define variable vlreturn as logical no-undo.
define variable vhDoc as handle no-undo.
define variable vhRoot as handle no-undo.


define temp-table ttBody xml-node-name "soapenv:Body"
field id as integer serialize-hidden.

define temp-table ttRequestMsg namespace-prefix "req"
xml-node-name "RequestMsg"
field id as integer serialize-hidden
field RequestMsg as character xml-node-type "text".


define dataset dsEnvelope xml-node-name "soap:Envelope"
for ttBody,
ttRequestMsg
data-relation r06 for ttBody, ttRequestMsg relation-fields (id, id) nested.

create ttBody.
assign
ttBody.id = 1.

create ttRequestMsg.
assign
ttRequestMsg.id = ttBody.id.

ttRequestMsg.RequestMsg = '<![CDATA[<Result><ResultType>Completed</ResultType></Result>]]>'.

vlreturn = dataset dsEnvelope:write-xml("longchar", /* file | stream | stream-handle | memptr | handle | longchar */
vlcxml,
yes, /* formatted */
"utf-8", /* encoding */
?, /* schema-location */
no, /* write-xmlschema */
no, /* min-xmlschema */
no, /* write-before-image */
yes). /* omit-initial-values */

create x-document vhDoc.
vhDoc:suppress-namespace-processing = true.
create x-noderef vhRoot.

vhDoc:load("longchar", vlcxml, false).

vhDoc:get-document-element(vhRoot).
vhRoot:set-attribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/").
vhRoot:set-attribute("xmlns:req", "http://api-v1.gen.mm.vodafone.com/mminterface/request").
vhDoc:save("file", "C:\Users\AntonyKyaloMusyoki\OneDrive\Desktop\NotePad++\SafaricomSOAP\sampleRequest.xml").

Here is the XML generated

<?xml version="1.0" encoding="UTF-8" ?>
<soap:Envelope xmlns:req="http://api-v1.gen.mm.vodafone.com/mminterface/request" xmlns:soap="" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body xmlns:soapenv="">
<RequestMsg>&lt;![CDATA[&lt;Result&gt;&lt;ResultType&gt;Completed&lt;/ResultType&gt;&lt;/Result&gt;]]&gt;</RequestMsg>
</soapenv:Body>
</soap:Envelope>
 

Stefan

Well-Known Member
Last edited:
Top