Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I solved this using an <a href="https://x-stream.github.io/converter-tutorial.html" rel="nofollow noreferrer">XStream Converter</a>. It checks for the <code>@XmlType</code> annotation to determine if a JAXB bean is being converted. All other types go through the default converters.</p> <p>Though a JAXB-centric solution would have been nice, XStream provided a compellingly straightforward solution.</p> <pre><code>public class FlatXmlConverter implements Converter { private static final Logger log = LoggerFactory.getLogger(NvpConverter.class); @Override public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { Class&lt;? extends Object&gt; sourceClass = source.getClass(); String prefix = (String) context.get("prefix"); for (Field field : sourceClass.getDeclaredFields()) { if (!field.isAccessible()) { field.setAccessible(true); } String name = field.getName(); Class&lt;?&gt; type = field.getType(); try { Object value = field.get(source); if (value != null) { if (type.isAnnotationPresent(XmlType.class)) { context.put("prefix", name); context.convertAnother(value); context.put("prefix", null); } else { String nodeName; if (prefix == null) { nodeName = name; } else { nodeName = prefix + "_" + name; } writer.startNode(nodeName); context.convertAnother(value); writer.endNode(); } } } catch (IllegalArgumentException ex) { log.error("IllegalArgumentException", ex); } catch (IllegalAccessException ex) { log.error("IllegalAccessException", ex); } } } @Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { throw new UnsupportedOperationException("Not supported yet."); } @Override @SuppressWarnings({"rawtypes", "unchecked"}) public boolean canConvert(Class type) { log.debug("canConvert({})", type); return type.isAnnotationPresent(XmlType.class); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload