Encoding in POJO to/from XML conversion within Camel - xml-parsing

We have been very successful to carry out POJO to/from XML conversion within Camel. The following code exemplifies a typical case how we use Camel. Our application listens to an Oracle AQ. The queue entry is an xml String. The xml is then converted to POJO class (MyClass), we then do some transformation on the MyClass with data from other source. After this transformation, POJO object is converted back to a string and sent to other system (here we save to a file)
<route id="testing">
<from uri="oracleaq:queue:FUSEQUEUE"/>
<convertBodyTo type="generated.MyClass"/>
<bean ref="mainReqprocessor" method="Modify"/>
<convertBodyTo type="java.lang.String"/>
<setHeader headerName="Exchange.FILE_NAME">
<simple>output.xml</simple>
</setHeader>
<to uri="file:C:\\Temp\\OUT"/>
</route>
Everything works fine until yesterday when we introduced html tags into one of the text field of the POJO class. We wrapped the text with CData "<![CDATA[" + str + "]]>". But, when the POJO is converted to string, the encoding still occurred on the starting and ending brackets of CGata section, such as the following. Because of this, the resulting xml string is not valid xml any more, and therefore can not be converted back to MyClass for other application. This is not the desired behavior. How can I avoid the encoding on CDATA starting and ending brackets?[Notes: the first < and the last > in the cdata are encoded.]
<TEXT>
&lt;![CDATA[&lt;html&gt;&lt;div&gt;&lt;pre&gt;COMPONENT PARTS.&lt;/br&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/html&gt;]]&gt;
<\/TEXT>

Although you have a marshalling/unmarshalling problem, you don't mention how you convert the XML to POJO and back. This would be a very important information to help.
If you are using JAXB for the conversion, this Q/A could perhaps help you:
JAXB Marshalling Unmarshalling with CDATA

Related

How to provide an empty Source in xslTransformer.transform() method?

I have an xslt 2.0 file which is being used to transform a csv file to an xml file. The xsl has been taken from here:
http://p2p.wrox.com/xslt/40898-transform-csv-file-xml.html#post164344
Now I am trying to execute this through Java transformer (using the Saxon9 xsl transformer factory). Since the csv file is being passed into the xsl as a parameter, there is no need for me to pass anything in the Source parameter in the transform method. Since the javadocs for the transform method state the following:
The javadocs for the Transformer.transform method clearly state that the following:
"An empty Source is represented as an empty document as constructed by DocumentBuilder.newDocument(). The result of transforming an empty Source depends on the transformation behavior; it is not always an empty Result."
I tried to create an empty document and try the transformation as seen below:
TransformerFactory transformerFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
Source xsltSource = new StreamSource("file:///C:/my.xsl");
Transformer xsltTransformer = transformerFactory.newTransformer(xsltSource);
xsltTransformer.setParameter("pathToCSV", "'file:///C:/input.csv'");
StringWriter writer = new StringWriter();
xsltTransformer.transform(new DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()), new StreamResult(writer));
The above piece of code does not output anything and does not work as expected since I think the empty document given as input is taken into consideration rather than the csv file passed in the following line in the xsl:
<xsl:param name="pathToCSV" />
<xsl:variable name="input" select="unparsed-text($pathToCSV)"/>
Could anyone give me pointers on how to accomplish what I am trying to achieve?
Consider to use the Saxon API http://saxonica.com/documentation/html/using-xsl/embedding/s9api-transformation.html and not to use the JAXP API if you want to use XSLT 2.0 features like starting with a named template as the XSLT you linked to requires. Or, if you want to use JAXP with an empty dummy document you at least need to add a template doing
<xsl:template match="/">
<xsl:call-template name="main"/>
</xsl:template>

importing a wsdl file in delphi XE3

I have to address a webservice with SOAP.
the WSDL file is http://www.healthconnect.be/sites/default/files/support/ConsultMyCareNet.wsdl
However when I import this WSDL file in my project (Delphi XE3) with file- new - other - WSDL importer there are some faults,
In top of the new *.pas file I find:
The following types, referred to in the WSDL document are not being represented
in this file. They are either aliases[#] of other types represented or were referred
to but never[!] declared in the document. The types from the latter category
typically map to predefined/known XML or Embarcadero types; however, they could also
indicate incorrect WSDL documents that failed to declare or import a schema type.*
// !:GetMCNStatusRequest - "urn:be:healthconnect:rrnconnector:ws:1_0:messages"
// !:GetMCNStatusResponse - "urn:be:healthconnect:rrnconnector:ws:1_0:messages"**
and thus the following important and necessary function for addressing the webservice cannot be compiled
with the message:
Cannot unwrap:
- Input part does not refer to an element
- Output part does not refer to an element*
function getMCNStatus(const parameters: GetMCNStatusRequest): GetMCNStatusResponse; stdcall;
But I need this function 'getMCNStatus' as It is a the first function to call since this function tests if the connection with the webservice is Ok. Without this function the rest does not work.
So how can I make this function work anyway?
Must I define the classes GetMCNStatusRequest and GetMCNStatusResponse myself and what Do I have to define in the classes ? Can I find the answer in the wsdl file ?
open your wsdl file.
Remove following lines within types tag.
<xs:import namespace="urn:be:healthconnect:rrnconnector:ws:1_0:person" schemaLocation="person-1-0.xsd" />
<xs:import namespace="urn:be:healthconnect:rrnconnector:ws:1_0:common" schemaLocation="common-1-0.xsd" />
<xs:import namespace="urn:be:healthconnect:rrnconnector:ws:1_0:messages" schemaLocation="messages-1-0.xsd" />
Now , you won't get any error while importing wsdl.
Update: Reason
Why is it happening?
Instead of defining element types in your wsdl file, you are importing these namespaces which has defined xsd files like person-1-0.xsd or common-1-0.xsd Which is not accessible. These namesapaces should be like
"http://healthconnect/example"

Deserialize XML with UTF-16 encoding in ServiceStack.Text

I am trying to use ServiceStack.Text to deserialize some XML.
Code:
var buildEvent = dto.EventXml.FromXml<TfsEventBuildComplete>();
The opening xml line is:
<?xml version="1.0" encoding="UTF-16"?>
ServiceStack fails with the following error:
The encoding in the declaration 'utf-16' does not match the encoding of the document 'utf-8'.
I can see from the source of the Xml Serializer that ServiceStack uses UTF-8.
I am wondering whether ServiceStack.Text can deserialize UTF-16 and if so how? And if not, why not?
I have managed to hack my way around the issue. I'm not proud of it but....
var buildEvent = dto.EventXml.Replace("utf-16", "utf-8").FromXml<TfsEventBuildComplete>();

Simple NSData's category to parse XML with cyrillic

I have to parse NSData with XML string, does somebody know simple category to do it? I have such for JSON, but I forced to use XML. I tried to use XMLReader, it's interface looks clean, but I found some issues:
Mysterious new line characters and spaces everywhere:
"comment_count" = {text = "\n \n 21";};
My cyrillic symbols looks so:
"description_text" = {text = "\n \U041f\U0438\U043a\U0430\U0431\U0443\U0448};
Example:
<?xml version="1.0" encoding="UTF-8" ?>
<news>
<xml_count>43</xml_count>
<hot_count>449</hot_count>
<item type="text">
<id>1469845</id>
<rating>147</rating>
<pluses>171</pluses>
<minuses>24</minuses>
<title>
<![CDATA[Обновление огромного архива Пикабу!]]>
</title>
<comment_count>26</comment_count>
<comment_link>http://pikabu.ru/story/obnovlenie_ogromnogo_arkhiva_pikabu_1469845</comment_link>
<author>icq677555</author>
<description_text>
<![CDATA[Пикабушники, я обновил свой огромный архив текстовых постов из горячего!]]>
</description_text>
</item>
</news>
I just realized whats' going on. Your data samples are obviously NSDictionary instances printed in the debugger. So the issues you found are:
As XML was originally designed as an annotated text format, the whitespace (spaces, newlines) handling doesn't perfectly fit for data only usage. You can either trim all resulting strings ([stringVar stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]), adapt XMLReader to do it or use the XML parser at http://ios.biomsoft.com/2011/09/11/simple-xml-to-nsdictionary-converter/ (which does it by default).
The funny output you get for Cyrillic characters is the proper escaping for non-ASCII characters in the debugger output (which uses the old-style property list format). It's an artifact of the debugger output. Your variables contain the proper characters.
BTW: While JSON contains implicit type information (strings are always quoted, numbers are never quoted etc.), XML without a schema file does not. So all the parsed simple values will be strings even if they originally were numbers.
Update:
The XML parser you're using still contains the old whitespace handling code described in Pesky new lines and whitespace in XML reader class (though the comment tells otherwise). Apply the fix mentioned at the bottom of the answer, namely change the line:
[dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
to:
[dictInProgress setObject:[textInProgress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:kXMLReaderTextNodeKey];

retrieve xml file using nsxmlparser in ios

i am getting problem while reading xml files through nsxmlparser in ios,
<PRODUCTS>
<PRODUCTSLIST>
<PRODUCTDETAILS>
<headertext> test header </headertext>
<description><b style="font-size: x-small;">product, advantages</b></description>
</PRODUCTDETAILS>
</PRODUCTSLIST>
</PRODUCTS>
while i read the file using nsxmlparser i am able to get value(test header) for headertext but the description attribute value contains html tags so i cant able to get the result (<b style="font-size: x-small;">product, advantages</b>)i am getting result as empty
How can i get the result as((<b style="font-size: x-small;">product, advantages</b>)) for description attribute?
Speaking from a developers perspective I would not recommend using NSXMLParser due to it's laborious way to parse XML Files. There is a great write up about choosing the right XML Parser.
I use KissXML quite often.
You can find a quit tutorial of using it here.
Hope this helps.
Your problem is that the "b" tag is considered part of the XML structure, try escaping the '<' and '>' characters of the 'b' tag:
#"<b style=\"font-size: x-small;>product, advantages</b>"
see here

Resources