org.sakaiproject.tool.assessment.qti.util
Class XmlMapper

java.lang.Object
  extended byorg.sakaiproject.tool.assessment.qti.util.XmlMapper

public class XmlMapper
extends Object

Utility class. Maps XML elements and attribute under a given node to a Map, or populates bean. Note this now supports deep copy.

Version:
$Id: XmlMapper.java 1820 2005-09-19 18:34:07Z esmiley@stanford.edu $
Author:
@author Ed Smiley

Field Summary
static String ATTRIBUTE_PREFIX
           
 
Constructor Summary
XmlMapper()
           
 
Method Summary
static Map map(Document doc)
          Maps each element node and attribute under a given node to a Map.
static void populateBeanFromDoc(Serializable bean, Document doc)
          Maps each element node to a bean property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTRIBUTE_PREFIX

public static final String ATTRIBUTE_PREFIX
See Also:
Constant Field Values
Constructor Detail

XmlMapper

public XmlMapper()
Method Detail

map

public static Map map(Document doc)
Maps each element node and attribute under a given node to a Map. It associates each element's text value with its name, and each attribute value with a key of "attribute_" + the attribute name. If node is a document it processes it as if it were the root node. If there are multiple nodes with the same element name they will be stored in a List. NOTE: This was DESIGNED to ignore elements at more depth than root +1. It has now been modified to deep copy under the nodes, but it WILL NOT recurse and assign key value pairs below, this is by design. The elements below (e.g. XML snippets, XHTML text) are all put into the value.

Returns:
HashMap

populateBeanFromDoc

public static void populateBeanFromDoc(Serializable bean,
                                       Document doc)
Maps each element node to a bean property. Supports only simple types such as String, int and long, plus Lists. If node is a document it processes it as if it were the root node. If there are multiple nodes with the same element name they will be stored in a List. NOTE: This is DESIGNED to ignore elements at more depth so that simple String key value pairs are used. This WILL NOT recurse to more depth, by design. If it did so, you would have to use maps of maps.

Parameters:
bean - Serializable object which has the appropriate setters/getters
doc - the document