Class 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$
    Author:
    , Ed Smiley
    • 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.
        Parameters:
        node - Node
        indent - String
        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