0

I'm converting XML code to a Java Map. The XML matches a large number of random words with a number (a probability distribution) and looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
   <Durapipe type="int">1</Durapipe>
   <EXPLAIN type="int">2</EXPLAIN>
   <woods type="int">2</woods>
   <hanging type="int">3</hanging>
   <hastily type="int">2</hastily>
   <localized type="int">1</localized>
   .......
</root>

I'm trying to implement this with XStream. Here's the Java code that my main program currently uses:

    XStream xstream = new XStream();
    Map<String, Integer> englishCorpusProbDist; 
    xstream.registerConverter(new MapEntryConverter());
    englishCorpusProbDist = (Map<String, Integer>)xstream.fromXML(new File("C:/Users/David Naber/Documents/IREP Project/frequencies.xml"));

And here's my MapEntryConverterClass:

public class MapEntryConverter implements Converter {
  public boolean canConvert(Class clazz) {
    return Map.class.isAssignableFrom(clazz);
  }  

  public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
    Map<String, Integer> map = (Map<String, Integer>) value;
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
      writer.startNode(entry.getKey().toString());
      writer.setValue(entry.getValue().toString());
      writer.endNode();
    }
  }

  public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Map<String, Integer> map = new HashMap<String, Integer>();

    while (reader.hasMoreChildren()) {
      reader.moveDown();
      map.put(reader.getNodeName(), reader.getValue());
      reader.moveUp();
     }
    return map;
   }
}

I"m getting an error in the above function, on the line "map.put(reader.getNodeName(), reader.getValue());". The error says: "The method put(String, Integer) in the type Map is not applicable for the arguments (String, String)."

So I really have two questions here. First of all, why is this error happening and how can I fix it? Secondly, what more will I need to implement to finally get XStream to convert this to XML?

Any help is much appreciated. Thank you in advance!

2 Answers 2

1

Yes error is correct reader.getValue() is giving String , You must have to Type Cast it in Integer

Change below code

map.put(reader.getNodeName(), reader.getValue());

to

map.put(reader.getNodeName(), new Integer(reader.getValue()));
Sign up to request clarification or add additional context in comments.

2 Comments

Okay. I did that and it got rid of the error and compiled correctly. However, I was given a new exception that said, "Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: root". Any suggestions for this?
0

This is my example for more complex data with nested Maps

  public static class MapEntryConverter implements Converter {
    static final Converter INSTANCE = new MapEntryConverter();

    public boolean canConvert(Class clazz) {
      return Map.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
      Map map = (Map) value;
      for (Object obj : map.entrySet()) {
        Map.Entry entry = (Map.Entry) obj;
        writer.startNode(entry.getKey().toString());
        Object val = entry.getValue();
        if (val != null) context.convertAnother(val);
        writer.endNode();
      }
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      Map<String, Object> map = new LinkedHashMap<String, Object>();

      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String key = reader.getNodeName();
        Object value = null;

        if (reader.hasMoreChildren()) {
          value = unmarshal(reader, context);
        } else {
          value = reader.getValue();
        }

        map.put(key, value);
        reader.moveUp();
      }

      return map;
    }
  }

Have fun!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.