Escaping Special Characters with JiBX (Un) Marshalling

    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:

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.

  • Share/Bookmark
Tech Notes

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)