Announcement Announcement Module
Collapse
No announcement yet.
HashMap mapped Bean to XML Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HashMap mapped Bean to XML

    Is it possible to write a XML file from a HashMap?

    Ex.

    {id=100, name="Example", address="Rue Morgue"}

    output...

    Code:
    <bean>
       <id>100</id>
       <name>Example</name>
       <address>Rue Morgue</address>
    <bean>
    Thanks

  • #2
    I found a solution. Hope can help someone.
    Useful when you don't want to be bound to some JavaBean for database extraction stuff. So you don't need to recompile.
    Is it possible to use a org.springframework.jdbc.core.ColumnMapRowMapper in JDBCCursorItemReader that extracts database values creating a LinkedHashMap for every record.

    Code:
    <bean id="itemMarshallerMap" class="org.springframework.oxm.xstream.XStreamMarshaller">
    				<property name="aliases">
    					<map>
    						<entry key="customer" value="java.util.LinkedHashMap"/>
    					</map>
    				</property>
    				<property name="converters">
                                      <list>
                                         <bean class="com.primeur.writer.XMLMapConverter"/>
                                     </list>
                                   </property>
                    
    			</bean>

    Code:
    public class XMLMapConverter implements Converter {
    
        
        public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
            Map<String, String> map = new HashMap<String, String> ();
            populateMap(reader, context, map);
            return map;
        }
    
        private void populateMap(HierarchicalStreamReader reader, UnmarshallingContext context, Map<String, String> map) 
        {
            for(; reader.hasMoreChildren(); reader.moveUp()) 
            {
                reader.moveDown();
                String key = reader.getNodeName();
                String value = reader.getValue();
                map.put(key, value);
            }
        }
    
        public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) 
        {
            Map<?,?> map = (LinkedHashMap<?,?>) source;
            if(map == null)
                return;
            Iterator<?> keysIterator = (Iterator<?>) map.keySet().iterator();
            String key = null;
            String value = null;
            
            while(keysIterator.hasNext())
            {
                key = (String) keysIterator.next();
                
                if (map.get(key) != null) 
                {
                    value = map.get(key).toString();
                    
                    writer.startNode(key);
                    writer.setValue(value);
                    writer.endNode();
                    
                }
                value = null;
            }
        }
    
        public boolean canConvert(Class type) {
            return true;
        }
    }

    Comment

    Working...
    X