Spring Object/XML Marshalling support providing generic interfaces for converting Java objects to XML and vice versa
The core marshalling interfaces provide the foundation for all XML marshalling and unmarshalling operations in Spring OXM. These interfaces define generic contracts that can be implemented by various XML processing technologies.
The primary interface for converting Java objects to XML.
public interface Marshaller {
/**
* Indicate whether this marshaller can marshal instances of the supplied type.
* @param clazz the class that this marshaller is being asked if it can marshal
* @return true if this marshaller can indeed marshal instances of the supplied class; false otherwise
*/
boolean supports(Class<?> clazz);
/**
* Marshal the object graph with the given root into the provided Result.
* @param graph the root of the object graph to marshal
* @param result the result to marshal to
* @throws IOException if an I/O error occurs
* @throws XmlMappingException if the given object cannot be marshalled to the result
*/
void marshal(Object graph, Result result) throws IOException, XmlMappingException;
}The primary interface for converting XML to Java objects.
public interface Unmarshaller {
/**
* Indicate whether this unmarshaller can unmarshal instances of the supplied type.
* @param clazz the class that this unmarshaller is being asked if it can marshal
* @return true if this unmarshaller can indeed unmarshal to the supplied class; false otherwise
*/
boolean supports(Class<?> clazz);
/**
* Unmarshal the given Source into an object graph.
* @param source the source to marshal from
* @return the object graph
* @throws IOException if an I/O error occurs
* @throws XmlMappingException if the given source cannot be mapped to an object
*/
Object unmarshal(Source source) throws IOException, XmlMappingException;
}Extended interfaces that provide support for generic types and parameterized types.
public interface GenericMarshaller extends Marshaller {
/**
* Indicates whether this marshaller can marshal instances of the supplied generic type.
* @param genericType the type that this marshaller is being asked if it can marshal
* @return true if this marshaller can indeed marshal instances of the supplied type; false otherwise
*/
boolean supports(Type genericType);
}
public interface GenericUnmarshaller extends Unmarshaller {
/**
* Indicates whether this unmarshaller can unmarshal instances of the supplied generic type.
* @param genericType the type that this unmarshaller is being asked if it can unmarshal
* @return true if this unmarshaller can indeed unmarshal instances of the supplied type; false otherwise
*/
boolean supports(Type genericType);
}import org.springframework.oxm.Marshaller;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
// Assuming marshaller is configured
Marshaller marshaller = // ... get marshaller instance
// Check if marshaller supports the type
if (marshaller.supports(MyClass.class)) {
MyClass object = new MyClass();
StringWriter writer = new StringWriter();
// Marshal to XML
marshaller.marshal(object, new StreamResult(writer));
String xmlResult = writer.toString();
}import org.springframework.oxm.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
// Assuming unmarshaller is configured
Unmarshaller unmarshaller = // ... get unmarshaller instance
String xmlData = "<myClass>...</myClass>";
// Check if unmarshaller supports the type
if (unmarshaller.supports(MyClass.class)) {
StringReader reader = new StringReader(xmlData);
// Unmarshal from XML
MyClass result = (MyClass) unmarshaller.unmarshal(new StreamSource(reader));
}import org.springframework.oxm.GenericMarshaller;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
GenericMarshaller genericMarshaller = // ... get generic marshaller
// Create a parameterized type for List<MyClass>
ParameterizedType listType = new ParameterizedType() {
public Type[] getActualTypeArguments() { return new Type[]{MyClass.class}; }
public Type getRawType() { return List.class; }
public Type getOwnerType() { return null; }
};
// Check generic type support
if (genericMarshaller.supports(listType)) {
List<MyClass> objectList = Arrays.asList(new MyClass(), new MyClass());
// Marshal the generic collection...
}import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.GenericMarshaller;
import org.springframework.oxm.GenericUnmarshaller;
import org.springframework.oxm.XmlMappingException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.lang.reflect.Type;All marshalling operations can throw XmlMappingException or its subclasses. Common scenarios include:
Always handle these exceptions appropriately in your application code.
Install with Tessl CLI
npx tessl i tessl/maven-org-springframework--spring-oxm