Skip to content

Instantly share code, notes, and snippets.

@fairjm
Last active December 7, 2015 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fairjm/5359192cc3dfd637ecd0 to your computer and use it in GitHub Desktop.
Save fairjm/5359192cc3dfd637ecd0 to your computer and use it in GitHub Desktop.
XStream handle map with key value pair with CDATA embeded
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
/**
* @author fairjm
*
**/
public class XmlMapUtil {
private static final MapConverter MAP_ENTRY_CONVERTER = new MapConverter();
private static final String NODE_START = "<%s>";
private static final String NODE_END = "</%s>";
private static final String VALUE_WITH_CDATA = "<![CDATA[%s]]>";
private static final String DEFALUT_ROOT_NAME = "xml";
public static String toXml(final Map<String, String> map, String rootName,
final boolean withCData) {
rootName = StringUtils.isBlank(rootName) ? DEFALUT_ROOT_NAME : rootName;
final StringBuilder sb = new StringBuilder();
sb.append(String.format(NODE_START, rootName));
if (map != null) {
for (final Entry<String, String> entry : map.entrySet()) {
sb.append(String.format(NODE_START, entry.getKey()));
sb.append(
withCData ? String.format(VALUE_WITH_CDATA, entry.getValue()) : entry.getValue());
sb.append(String.format(NODE_END, entry.getKey()));
}
}
sb.append(String.format(NODE_END, rootName));
return sb.toString();
}
public static Map<String, String> fromXml(final String xml, final String rootName) {
Map<String, String> map = null;
try {
final XStream xStream = new XStream();
xStream.alias(rootName, Map.class);
xStream.registerConverter(MAP_ENTRY_CONVERTER);
map = (Map<String, String>) xStream.fromXML(xml);
} catch (final Exception e) {
e.printStackTrace();
}
return map;
}
public static class MapConverter implements Converter {
@Override
public boolean canConvert(final Class clazz) {
return Map.class.isAssignableFrom(clazz);
}
@Override
public void marshal(final Object value, final HierarchicalStreamWriter writer, final MarshallingContext context) {
@SuppressWarnings("unchecked")
final
Map<String, String> map = (Map<String, String>) value;
for (final Entry<String, String> entry : map.entrySet()) {
writer.startNode(entry.getKey().toString());
final Object val = entry.getValue();
if (null != val) {
writer.setValue(val.toString());
}
writer.endNode();
}
}
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
final Map<String, String> map = new HashMap<String, String>();
while (reader.hasMoreChildren()) {
reader.moveDown();
final String key = reader.getNodeName();
final String value = reader.getValue();
map.put(key, value);
reader.moveUp();
}
return map;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment