How to unescape a CDATA section using JAXB in a dynamic way - jaxb

I'm trying to make a section of my SOAP request look like this:
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><placeholder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="WirelessEdgeInfo_v1_0.xsd"/>]]>
However, it looks like this after it has been escaped by underlying JAXB:
<![CDATA[<?xml version="1.0"
encoding="UTF-8"?> <placeholder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="WirelessEdgeInfo_v1_0.xsd"/>]]&gt
I have looked at numerous examples, such as using MOXy and also implementing a CharacterEscapeHandler adapter or XmlAdapter HOWEVER the problem is the same for ALL these implementations. That is, it always comes down to this:
marshaller.setProperty(CharacterEscapeHandler.class.getName(),
new XmlCharacterHandler());
Setting a property in the marshaller.
However, in my case, I am using annotations generated through WSImport to generate my Web Services code dynamically when I execute the client SOAP call.
i.e. I do not have access to the marshaller object.
SO for me, how can I unescape the CDATA section of a String variable I need to pass in if I cannot set any property on the marshaller type?
Can I somehow "hack" or trick JAXB to tell it not to escape CDATA values? Or can I use annotations or somehow dynamically indicate unescaping?
I tried the following Adapter via annotation but of course this did not work:
#XmlJavaTypeAdapter(value=CDATAAdapter.class) //this is not working right now
protected String variableXmlContent;
variableXmlContent still ended up escaped. I also tried unescaping variableXmlContent and setting it as an input value, but it still came out the same way, unescaped.

An XML element value containing <, >, & is escaped by the marshaller using <, >, &.
Thus, if the XML element should contain
<![CDATA[ ... ]]>
the result in the XML file will be
<![CDATA[ ... ]]>
I suspect that the text to be passed as an XML element is
<?xml version="1.0" ... v1_0.xsd"/>
Now if I had to write this by hand into an XML file, I'd write it into a "CDATA section", but a (JAXB) marshaller will simply treat those three characters as I described before, and that's it.
<?xml version="1.0" ... v1_0.xsd"/>
It is pointless to try and make any XML marshaller use the CDATA mechanism, and there is no such thing as a "CDATA value" requiring special treatment - so there is no such special treatment.

Related

How to do XML-Split and Keep Parent Element?

I know Spring Integration is rather old but I have a piece of code that needs small modification to move on. I need to split the incoming XML message and preserve the parent node. Here is my XML:
<Order>
<Item>A</Item>
<Item>B</Item>
</Order>
And I want to split into 2 XMLs and preserve the XSD validation.
<Order>
<Item>A</Item>
</Order>
and
<Order>
<Item>B</Item>
</Order>
Did this:
<beans xmlns:int-xml="http://www.springframework.org/schema/integration/xml">
...
<int-xml:xpath-splitter id="splitter">
<int-xml:xpath-expression expression="/Order/Item"/>
</int-xml:xpath-splitter>
...
It split into
<Item>A</Item>
and
<Item>B</Item>
but I need the parent to be there. Anyway to make it works? Thanks in Advance.
What you need is an <int-xml:xslt-transformer> after your <int-xml:xpath-splitter>.
See Reference Manual for more info: https://docs.spring.io/spring-integration/docs/5.2.0.RELEASE/reference/html/xml.html#xml-xslt-payload-transformers
You need to learn what is XSLT and so on.
I know Spring Integration is rather old
??
It is actively maintained and enhanced; there was a completely new release this month.
You either need a custom splitter, or you can add a transformer after the splitter to add the outer node.

Using retrofit to make a #Post but sending a xml body content in Kotlin?

I am very new at Kotlin. I am wondering if this is possible to do it in Kotlin.
I have a request that is basically a POST to an endpoint, it sends information as part of the body, something like the following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE...">
<some more tags...>
<request>
<clientAuth>
<client>
{client-id}
</client>
</cientAuth>
....
So I basically received some parameters (Like client-id) and I need to form this xml (as far as I am doing it at postman) and send the request.
Checking some libraries I noticed with retrofit is sort of possible, but I have no idea, is it really possible?
I know for "form url enconder" requests is simpler by using #FormUrlEncoded but not sure if there is one for xmls...
So far I got this:
an interface...
#POST("user-endpoint")
fun clientRequest(
#Field("clientId") clientId: String
): Deferred<Response<Void>>
and I invoke clientRequest from other function.
Testing the logic works fine for endpoints without parameters, but I have no idea how to send that xml, is it even possible?
I find out I can use tickaroo to represent my body as an object and then use retrofit without problem.

For standalone SVG(.svg files), is it mandatory to include the the DOCTYPE and the XML declaration?

For standalone SVG(.svg files), is it mandatory to include the the DOCTYPE and the XML declaration ?
A doctype is not required some people recommend not using it
In XML 1.1 the XML declaration is mandatory though. If it's omitted the document is supposed to be treated as XML 1.0 which may or may not be what you wanted. Note that browsers don't enforce this requirement so if that's your only use case you can omit the XML declaration.

set Jaxb encoding in Mule

I'm running Mule 3.8 CE and my JAXB context and marshaller always create files declared like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
However the files are always UTF-16, and they get marshalled to utf-16, but the declaration says otherwise and I haven't found a way to solve that. I'm not sure how to set properties on my marshaller in Mule?
Feels a little stupid to have an external script just for replacing that string.
Regards

Facelet does not convert formatted currency correctly

I have the follwing code inside a facelet page:
<h:inputNumber value="bean.property">
<f:convertNumber type="currency" />
</h:inputNumber
The converter is because there can be a kind of default value inside the input field, which comes from the bean property. Everything is rendered correctly. The value inside the input field is rendered with an "€" character (e.g. "1.453 €".
When I submit the form there comes an error up:
"nameOfInputField" konnte nicht als ein Geldbetrag erkannt werden '304,00 â¬'
In english it is some like:
"nameOfInputField" could not be regognized as an amount of money '304,00 â¬'
Please have a look at the "€" character. It seems to be printed as "â¬". While it was rendered correctly before submitting the form, now it looks like "â¬" inside the error message and inside the input field.
All pages are encoded in UTF-8.
What is the reason for this error?
How can fix it?
Thanks in advance
â¬
This is typical for the € from an original UTF-8 source which is incorrectly been decoded using ISO-8859-1. Here's a small snippet which demonstrates that:
System.out.println(new String("€".getBytes("UTF-8"), "ISO-8859-1"));
All pages are encoded in UTF-8.
You're likely talking about response encoding. You need to set the request encoding as well.
To set the encoding for GET requests (basically: URI encoding), you need to consult the appserver specific documentation. As it's unclear which one you're using, here's a Tomcat targeted example: <Connector URIEncoding="UTF-8" />. To set the encoding for POST requests, you need to create a simple filter which does request.setCharacterEncoding("UTF-8") if it is null. More background information and hints can be found in this article.
Put this ontop of your facelets page:
<?xml version="1.0" encoding="UTF-8" ?>
It will instruct the facelets parser.

Resources