views:

52

answers:

0

The DTO structure is like below-

class VO
{
Map<String,DTO> values;
}

class DTO
{
Map<String,String> details;
}

I am using XStream to convert the above DTO to XML and back to DTO

Without using any user defined converters in XStream, the converted XML looks like below-

<VO>
  <values>
    <entry>
      <string>input 123</string>
      <Dto>
        <attributes>
          <entry>
            <string>Status</string>
            <string>Complete</string>
          </entry>
          <entry>
            <string>revision</string>
            <string> a</string>
          </entry>         
        </attributes>
      </Dto>
    </entry>
  </values>
</VO>

To make the structure more readable and based on the requirement- wrote a user defined converter as below with the marshal method as below

public void marshal(Object value, HierarchicalStreamWriter writer,
            MarshallingContext context) {

        AbstractMap map = value instanceof VO? getInformation(value)
                : getAttributes(value);

        for (Object obj : map.entrySet()) {
            Entry entry = (Entry) obj;

            // If the map value is DTO- marshal the attributes
            // map again
            if (entry.getValue() instanceof DTO) {
                writer.startNode(entry.getKey().toString());
                marshal(entry.getValue(), writer, context);
                writer.endNode();
            } else {
                writer.startNode(entry.getKey().toString());
                writer.setValue(entry.getValue().toString());
                writer.endNode();
            }
        }
    }

Now, the new structure is,

<VO>
  <input 123>
    <status>complete</status>
    <revision>a</revision>
  </input 123>
</VO>

In the above code, we read each map entry, and making the key as the node name and the corresponding map value as the node value

I am facing issues while writing the counter part of unmarshal - I see only methods specific to attribute

And while using the method reader.getValue() - getting the below error

unexpected character in markup 4 (position: START_TAG seen )

Is a user defined converter required here? If so, how to handle the unmarshal part?

Thank you :)