org.omnaest.utils.xml
Class XMLNestedMapConverter

java.lang.Object
  extended by org.omnaest.utils.xml.XMLNestedMapConverter

public class XMLNestedMapConverter
extends Object

The XMLNestedMapConverter allows to convert xml content into a nested Map hierarchy.

Example:


The following xml snippet:
 <?xml version="1.0" encoding="UTF-8"?>
 <Books >
     <header>
         <metainfo>Some meta information</metainfo>
     </header>
     <Book>
         <Title>Simple title</Title>
         <author >
             an author
         </author>
     </Book>
     <Book>
         <Title>Second<![CDATA[ simple]]>
             title
         </Title>
         <Author >Second author</Author>
     </Book>
 </Books>
 
will be converted into following nested map hierarchy:
 -+
  |-+ Books
  | |-+ header
  | | |-- metainfo=Some meta information
  | |
  | |-- Book=[{Title=Simple title, author=
             an author
         }, {Title=Second simple
             title
         , Author=Second author}]
  |
 
 
(All '+' nodes represents a new Map object)

Note: the two similar named 'book' xml tags are merged into their parental Map using a single shared key but instead of simple primitive values the key points to a List of Objects.

Author:
Omnaest
See Also:
newMapFromXML(CharSequence), newNamespaceAwareMapFromXML(CharSequence), setExceptionHandler(ExceptionHandler)

Field Summary
static String DEFAULT_ENCODING
           
static XMLInstanceContextFactory XML_INSTANCE_CONTEXT_FACTORY_JAVA_STAX_DEFAULT
           
 
Constructor Summary
XMLNestedMapConverter()
           
 
Method Summary
 Map<String,Object> newMapFromXML(CharSequence xmlContent)
          Similar to newNamespaceAwareMapFromXML(CharSequence) but with non Namespace aware String values as keys.
protected
<K> Map<K,Object>
newMapFromXML(CharSequence xmlContent, ElementConverter<QName,K> keyElementConverter)
          Template method for newNamespaceAwareMapFromXML(CharSequence) and newMapFromXML(CharSequence) which allows to convert the QName based key values to other representations.
 Map<QName,Object> newNamespaceAwareMapFromXML(CharSequence xmlContent)
          Creates nested Map instances which contains all information from the given xml content as key value pairs.
 XMLNestedMapConverter setEncoding(String encoding)
          Sets another encoding.
 XMLNestedMapConverter setExceptionHandler(ExceptionHandler exceptionHandler)
           
 XMLNestedMapConverter setXmlInstanceContextFactory(XMLInstanceContextFactory xmlInstanceContextFactory)
          Allows to set an alternative XMLInstanceContextFactory
 String toNamespaceAwareXML(Map<QName,Object> nestedMap)
          Similar to toXML(Map) but for Map instances having a QName based key type
 void toNamespaceAwareXML(Map<QName,Object> nestedMap, OutputStream outputStream)
          Similar to toXML(Map, OutputStream) but for Maps having QNames as key type
 String toNamespaceAwareXMLDocument(Map<QName,Object> nestedMap)
          Similar to toNamespaceAwareXML(Map, OutputStream) but returns a String instance instead of writing into an OutputStream
 void toNamespaceAwareXMLDocument(Map<QName,Object> nestedMap, OutputStream outputStream)
          Similar to toXMLDocument(Map, OutputStream) but has full qualified QNames
 String toXML(Map<String,Object> nestedMap)
          Returns the xml content equivalent to the given nested Map structure
 void toXML(Map<String,Object> nestedMap, OutputStream outputStream)
          Similar to toXML(Map) but writes the result to a given OutputStream directly instead of creating a String.
 String toXMLDocument(Map<String,Object> nestedMap)
          Similar to toXMLDocument(Map, OutputStream) but returns a String instance instead of writing into an OutputStream
 void toXMLDocument(Map<String,Object> nestedMap, OutputStream outputStream)
          Similar to toXML(Map, OutputStream) but encloses the given nested Map into a xml document with a single root tag
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ENCODING

public static final String DEFAULT_ENCODING
See Also:
Constant Field Values

XML_INSTANCE_CONTEXT_FACTORY_JAVA_STAX_DEFAULT

public static final XMLInstanceContextFactory XML_INSTANCE_CONTEXT_FACTORY_JAVA_STAX_DEFAULT
Constructor Detail

XMLNestedMapConverter

public XMLNestedMapConverter()
Method Detail

newNamespaceAwareMapFromXML

public Map<QName,Object> newNamespaceAwareMapFromXML(CharSequence xmlContent)
Creates nested Map instances which contains all information from the given xml content as key value pairs.

QNames of the tag elements do represent the keys of the returned Map.
Values are Strings, Lists or Maps.

Attributes are mapped to key value pairs as they are. Sub tags containing only text data will be converted to key value pairs using their tag name as key and the textual information as value.

Parameters:
xmlContent -
Returns:
new (nested) Map instance
See Also:
XMLNestedMapConverter, newMapFromXML(CharSequence)

newMapFromXML

public Map<String,Object> newMapFromXML(CharSequence xmlContent)
Similar to newNamespaceAwareMapFromXML(CharSequence) but with non Namespace aware String values as keys. Those keys will only contain the tag name without any Namespace information.

Parameters:
xmlContent -
Returns:
new (nested) Map instance
See Also:
newNamespaceAwareMapFromXML(CharSequence)

newMapFromXML

protected <K> Map<K,Object> newMapFromXML(CharSequence xmlContent,
                                          ElementConverter<QName,K> keyElementConverter)
Template method for newNamespaceAwareMapFromXML(CharSequence) and newMapFromXML(CharSequence) which allows to convert the QName based key values to other representations.

Parameters:
xmlContent -
Returns:
new (nested) Map instance

toXML

public String toXML(Map<String,Object> nestedMap)
Returns the xml content equivalent to the given nested Map structure

Parameters:
nestedMap - Map
Returns:
xml content
See Also:
XMLNestedMapConverter, toNamespaceAwareXML(Map)

toNamespaceAwareXML

public String toNamespaceAwareXML(Map<QName,Object> nestedMap)
Similar to toXML(Map) but for Map instances having a QName based key type

Parameters:
nestedMap - Map
Returns:
xml content
See Also:
XMLNestedMapConverter, toXML(Map)

toXMLDocument

public String toXMLDocument(Map<String,Object> nestedMap)
Similar to toXMLDocument(Map, OutputStream) but returns a String instance instead of writing into an OutputStream

Parameters:
nestedMap - Map
Returns:
xml content
See Also:
XMLNestedMapConverter, toNamespaceAwareXML(Map), toXML(Map)

toXMLDocument

public void toXMLDocument(Map<String,Object> nestedMap,
                          OutputStream outputStream)
Similar to toXML(Map, OutputStream) but encloses the given nested Map into a xml document with a single root tag

Parameters:
nestedMap -
outputStream -
See Also:
toXML(Map, OutputStream), toNamespaceAwareXMLDocument(Map, OutputStream)

toNamespaceAwareXMLDocument

public String toNamespaceAwareXMLDocument(Map<QName,Object> nestedMap)
Similar to toNamespaceAwareXML(Map, OutputStream) but returns a String instance instead of writing into an OutputStream

Parameters:
nestedMap - Map
Returns:
xml content
See Also:
XMLNestedMapConverter, toNamespaceAwareXML(Map, OutputStream), toNamespaceAwareXMLDocument(Map, OutputStream)

toNamespaceAwareXMLDocument

public void toNamespaceAwareXMLDocument(Map<QName,Object> nestedMap,
                                        OutputStream outputStream)
Similar to toXMLDocument(Map, OutputStream) but has full qualified QNames

Parameters:
nestedMap -
outputStream -
See Also:
toXML(Map, OutputStream), toNamespaceAwareXMLDocument(Map)

toXML

public void toXML(Map<String,Object> nestedMap,
                  OutputStream outputStream)
Similar to toXML(Map) but writes the result to a given OutputStream directly instead of creating a String.

The OutputStream will not be closed by this method call.

Parameters:
nestedMap - Map
outputStream - OutputStream
See Also:
toNamespaceAwareXML(Map, OutputStream), toXMLDocument(Map), toNamespaceAwareXMLDocument(Map)

toNamespaceAwareXML

public void toNamespaceAwareXML(Map<QName,Object> nestedMap,
                                OutputStream outputStream)
Similar to toXML(Map, OutputStream) but for Maps having QNames as key type

Parameters:
nestedMap - Map
outputStream - OutputStream
See Also:
toXML(Map, OutputStream), toNamespaceAwareXMLDocument(Map, OutputStream)

setExceptionHandler

public XMLNestedMapConverter setExceptionHandler(ExceptionHandler exceptionHandler)
Parameters:
exceptionHandler - ExceptionHandler
Returns:
this

setEncoding

public XMLNestedMapConverter setEncoding(String encoding)
Sets another encoding. Default is "UTF-8".

Parameters:
encoding - the encoding to set
Returns:
this

setXmlInstanceContextFactory

public XMLNestedMapConverter setXmlInstanceContextFactory(XMLInstanceContextFactory xmlInstanceContextFactory)
Allows to set an alternative XMLInstanceContextFactory

Parameters:
xmlInstanceContextFactory - XMLInstanceContextFactory
Returns:
this
See Also:
XML_INSTANCE_CONTEXT_FACTORY_JAVA_STAX_DEFAULT


Copyright © 2013. All Rights Reserved.