/xmlmap

Simple Java conversion XML to Map.

Primary LanguageJava

= Java *XMLMap*

Simple Java conversion XML to Map.


==  Concept

The idea behind the Java *XMLMap* API is simple: parse any XML to a Map.


Imagine a simple XML:

	<value>123</value>

You can easily imagine that being mapped to the Map:

	{ value = 123 }


But, there is some complex cases like:

1. Complex types;
2. Attributes;
3. Element repetition (lists);
4. Namespaces.

But, you can also imagine some simple correlations:

1. The value becomes another Map;
2. The key for attributes becomes @<attribute-name> inside the element Map;
3. The value becomes a List;
4. The namespaces are treated like attributes and element names.


So, this complex XML:

	<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
		<soap:Body>
			<hotel id="1">
				<name code="HYT">Hyatt</name>
			</hotel>
			<hotel id="2">
				<name>Ibis</name>
			</hotel>
			<check test="true">OK</check>
		</soap:Body>
	</soap:Envelope>

Becomes this:

	{
		soap:Envelope={
			soap:Body={
				hotel=[
					{name=
						[	Hyatt,
							{@code=HYT} ],
						@id=1},
					{name=
						Ibis, 
						@id=2}
				],
				check=[OK, {@test=true}]}, 
				@xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/
		}
	}


All real values becomes String. So, you never face conversion issues.


This approach helps using script languages, like Scala or Groovy, because you can easily do:

	def firstHotelName = map['soap:Envelope']['soap:Body']['hotel'][0]['name']


This approach is also useful on Apache Camel, where you can easily use any scripting language to
parse the body of the message, if that message is a map.

This is an example using "simple":

	<convertBodyTo type="java.util.Map" />
	<setBody>
		<simple>${body[soap:Envelope][soap:Body]}</simple>
	</setBody>