• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

JIBX Binding for HashMap

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While binding HashMap I have tried all the below approaches, but not working. Need your inputs on it.


PROBLEM STATEMENT:

Xml ‘remitRefMap’ element has to be mapped to pojo ‘RefData’ field.

XML :

<refData>
               <RefType>Key1</RefType>
               <RefIdent>Value1</ RefIdent>
</ refData >
< refData >
               <RefType>Key2</RefType>
               <RefIdent>Value2</ RefIdent>
</refData>

POJO :

Public class Request
{
  public Map<String,String> remitRefMap;
}

APPROACH 1:

Using Custom Marshallers and UnMarshallers. We have custom Mappers already there for JIBX org.jibx.extras.HashMapperStringToComplex.

This can’t be used since it can be applied only for Map<String,Object> which is of complex type which doesn’t suit the requirement of us where we use primitive types as key and values.


APPROACH 2 :

Using collections. Collection should have some methods like ‘size-method’, ‘store-method,’ iter-method’ which is not supported for  java.util.map

Binding.xml ( Wrote custom serializers/deserializers)

<collection field="remitRefMap" usage="optional" create-type="java.util.HashMap">
       <value style="element" name="RefType" type="java.lang.String" serializer ="JIBXMapConverter.serializeMap" deserializer="JIBXMapConverter.deSerializeMap"/>
       <value style="element" name="RefIdent" type="java.lang.String" serializer ="JIBXMapConverter.serializeMap"
deserializer ="JIBXMapConverter.deSerializeMap"/>
</collection>



APPROACH 3:

Wrote custom Marshllers/Unmarshllers .

Bindings.xml

<structure  name="RefData" field = “remitRefMap ” usage="optional" type=”java.util.HashMap”/>

<mapping class="java.util.HashMap" unmarshaller ="JibxMapMapper" marshaller ="JibxMapMapper"/>

It is throwing exception saying

Marshaller not found for element ‘RefType’. Since mapping for the element ‘RefType’ not possible since ‘remitRefMap’ is already mapped to ‘refdata’.
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic