Escaping Special Characters with JiBX (Un) Marshalling - Tech Notes
Home » Tech Notes

Escaping Special Characters with JiBX (Un) Marshalling

19 January 2009 No Comment

    While using JiBX with Spring Web Services, following exception can occur while (un) marshalling the data.

org.springframework.oxm.jibx.JibxUnmarshallingFailureException: JiBX unmarshalling exception: Error parsing document (line -1, col -1); nested exception is org.jibx.runtime.JiBXException: Error parsing document (line -1, col -1)
at org.springframework.oxm.jibx.JibxUtils.convertJibxException(JibxUtils.java:52)
at org.springframework.oxm.jibx.JibxMarshaller.convertJibxException(JibxMarshaller.java:169)

Two possible reasons behind JiBX not recongnizing the input characters are:

  • Character encoding does not include all characters of data being processed.
  • Characters in data needs to be ignored (these are the special characters to be escaped)

Following two solutions worked for above problems respectively. Defining Correct Character Encoding: Define correct character encoding for your data in following (first) line of the binding xml. <?xml version=”1.0″ encoding=”UTF-8“?> Handling Data Differently: Second possible soution is treating the data special i.e. CDATA. CDATA tag is not new for the xml technology users. With JiBX the data can be handled as CDATA through by setting the ‘style’ attribute in binding xml as “cdata”. Following example sets the value in ‘mapping’ tag. <binding xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”          xsi:noNamespaceSchemaLocation=”file://binding.xsd“>     <namespace prefix=”ns1″ uri=”http://www.mysite.com/MySchemas” />     <mapping name=”FileRequest” ns=”http://www.mysite.com/MySchemas” class=”com.mysite.Address” ordered=”true” >         <value ns=”http://www.mysite.com/MySchemas” name=”FirstName” set-method=”setFirstName” get-method=”getFirstName” usage=”required”/>         <value ns=”http://www.mysite.com/MySchemas” field=”lastName” set-method=”setLastName” get-method=”getLastName” style=”cdata” usage=”optional”/>     </mapping> </binding>  There are few restrictions on usage of this attribute. Details can be found at JiBX: <value> Element.

 

More Related Posts in Tech Notes

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.