encoding XML

I'm currently working on a requirement where i have to store xml data in baas and since baas takes only json data we are trying to encode the xml and store it and decode in the GET call and sending the xml response back to client.

The issue im having here is related to CDATA in the xml payload.

When i try to encode the xml data i have to convert the XML object to string and for that i'm using https://estk.aenhancers.com/9%20-%20Integrating%20XML%20into%20JavaScript/the-xml-object.html which provides me methods to parse the XML and also convert the XML object to string.Now when i convert it to string i can see the CDATA as part of my trace session and then im encoding it but the momment i run decode function right, I cannot see the CDATA as part of my xml rather im seeing plain text.

other ways i've tried

toString(),encodeURIComponent(),decodeURIComponent()-->all these functions also had the same result

Is there any way I can retain the CDATA elemnt even after converting to string and encoding it.

Any hint on how we can do this is really helpful!!

0 6 745
6 REPLIES 6

When you say, BaaS I think you are referring to Apigee Baas, which has reached end-of-life and is no longer supported. I don't know your support status... but if you don't have a pecial support arrangement, you shouldn't be using API BaaS.

But let me suppose that you have some other store, which stores strings, but not necessarily strings that contain angle brackets and quotes and whatnot, like normally XML does. What would I do?

I might base64 encode it.

And you can do that with an AssignMessage policy. I think the static function is encodeBase64.

https://docs.apigee.com/api-platform/reference/message-template-intro#base64-encoding-functions

Now, regarding CDATA, I don't know. I haven't tried it. But the encode/decode should be reflexive: after an encode + a decode, you should get out exactly what you put in.

Thanks for the response @Dino-at-Google


yeah encode and decode is reflexive untill we parse the XML using.The momment we parse the XML and do any transaformation on the data The resulting payload do not have the CDATA tags like if i have below sample payload

<Mocker>

<ProxyName>efx-interconnect-sandboxtest</ProxyName>

<Sample><![CDATA[Lithographic commercial printing]]></Sample>

</Mocker>

When you parse the above xml using https://estk.aenhancers.com/9%20-%20Integrating%20XML%20into%20JavaScript/the-xml-object.html or with any other parser you will have the CDATA but the momment you try to encode/decode "Sample" element you will not see <![CDATA[ ]]] tag in your decoded form we will just see the text inside it

What you are describing is expected.

CDATA is just a way to escape a block of text in an XML document. For any XML document with a CDATA, there are alternative equivalent representations for the document that do not use CDATA. For example, consider a really simple document, like this:

<M>
  <S><![CDATA[A]]></S>
</M>

This alternative document stores an exactly equivalent XML infoset:

<M>
  <S>A</S>
</M>

As does this:

<M>
  <S>& #x41;</S>
</M>

And this:

<M>
  <S>& #65;</S>
</M>

(you must collapse spaces between the & and # in the above)

All are valid representations of the same information. Any system that processes XML must not care how the XML is serialized. The processor must not distinguish between the forms I just gave as examples. All of them should result in exactly the same in-memory representation of the document.

In your specific case, "Lithographic commercial printing" needs no CDATA, no escaping; there are no special characters there. So when Apigee deserializes the XML document, it just treats it as regular text. Totally valid.

I think maybe your assumptions are not correct.

cool...thanks for explanation