Hi guys,

I need to unmarshall an XML document to a
linkedHashMap<String,ColumnAttributes>

The XML document I get from the server system is containing the list of
objects:
<ColumnAttributes key="BSI_REFIN_INTR_BU_CURRY_AMT">
      <name>BSI_REFIN_INTR_BU_CURRY_AMT</name>
      <label>Refinancing interest amount in BU currency</label>
      <length>8.0</length>
      <format>NLNUM18.</format>
      <inFormat>NLNUM18.</inFormat>
  </ColumnAttributes>

As you can see, the key is an attribute of the ColumnAttributes Element.

I have tested this code:
new NamedMapConverter(LinkedHashMap.class,xstream.getMapper(),
"column", "key", String.class, *"column"*, ColumnAttributes.class, true,
false, xstream.getConverterLookup())

but it generates one element for the key and one for the value.

If I use this one:
new NamedMapConverter(LinkedHashMap.class,xstream.getMapper(),
"column", "key", String.class, *null*, ColumnAttributes.class, true, false,
xstream.getConverterLookup())

Here is the complete code:
XStream xstream = new XStream(new DomDriver());
        // XStream xstream = new XStream(new DomDriver());
        xstream.alias("columns",LinkedHashMap.class) ;
        xstream.alias("column",ColumnAttributes.class) ;
        ConverterLookup lookup = xstream.getConverterLookup() ;
        Converter conv =
xstream.getConverterLookup().lookupConverterForType(String.class) ;
        System.out.println(conv.canConvert(String.class)) ;
        xstream.registerConverter(
         new NamedMapConverter(LinkedHashMap.class,xstream.getMapper(),
"column", "key", String.class, null, ColumnAttributes.class, true, false,
xstream.getConverterLookup()
        ));
        // String xml = xstream.toXML(rulesBook.getColumnsAttributes());
        // System.out.println(xml);
        String xml="<columns><column
key=\"BSI_REFIN_INTR_BU_CURRY_AMT\"><name>BSI_REFIN_INTR_BU_CURRY_AMT</name><label>Refinancing
interest amount in BU
currency</label><length>8.0</length></column></columns>" ;
        LinkedHashMap<String,ColumnAttributes> test =
(LinkedHashMap<String,ColumnAttributes>)xstream.fromXML(xml) ;

I get the following error:
Exception in thread "main"
com.thoughtworks.xstream.converters.ConversionException: No
SingleValueConverter for key available
---- Debugging information ----
class               : java.util.LinkedHashMap
required-type       : java.util.LinkedHashMap
converter-type      :
com.thoughtworks.xstream.converters.extended.NamedMapConverter
path                : /columns
version             : 1.4.7
-------------------------------
at
com.thoughtworks.xstream.converters.extended.NamedMapConverter.getSingleValueConverter(NamedMapConverter.java:359)
at
com.thoughtworks.xstream.converters.extended.NamedMapConverter.populateMap(NamedMapConverter.java:293)
at
com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:92)
at
com.thoughtworks.xstream.converters.collections.MapConverter.unmarshal(MapConverter.java:87)
at
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at
com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at
com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1031)
at com.keinavo.cse.excel.ExcelReaderSample.main(ExcelReaderSample.java:65)



Is it possible to have the key as an attribute of the value element in the
XML using the NamedMapconverter?

If not, do you have any suggestion for me?

Any feedback is welcome..


TIA

Johann

Reply via email to