From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

In computer science, unmarshalling or unmarshaling refers to the process of transforming a representation of an object that was used for storage or transmission to a representation of the object that is executable. A serialized object which was used for communication can not be processed by a computer program. An unmarshalling interface takes the serialized object and transforms it into an executable form. Unmarshalling (similar to deserialization) is the reverse process of marshalling.


Usually XML objects are used when data needs to be transferred between processes, threads or systems[citation needed], because this results in shorter message wire format and efficient data transfers[citation needed]. Once the data is transferred back to a program or an application, it needs to be converted back to an executable object for usage.[citation needed] Hence, unmarshalling is generally used in the receiver end of the implementations of Remote Method Invocation (RMI) and Remote Procedure Call (RPC) mechanisms to unmarshal transmitted objects in an executable form.


JAXB or Java Architecture for XML Binding is the most common framework used by developers to marshal and unmarshal Java objects. JAXB provides for the interconversion between fundamental data types supported by Java and standard XML schema data types.[1]


XmlSerializer is the framework used by C# developers to marshal and unmarshal C# objects. One of the advantages of C# over Java is that C# natively supports marshalling due to the inclusion of XmlSerializer class. Java, on the other hand requires a non-native glue code in the form of JAXB to support marshalling.[2]

XML and Executable Representation[edit]

An example of unmarshalling is the conversion of an XML representation of an object to the default representation of the object in any programming language. Consider the following class.

public class Student
    private char name[50];
    private int ID;
    public String getName()
        return this.name;
    public int getID()
        return this.ID;
    void setName(String name)
        this.name = name;
    void setID(int ID)
        this.ID = ID;
  • XML representation of Student object:
<!-- Code Snippet: 1 -->

<?xml version = “1.0” encoding = “UTF-8”?>
    <student id = “11235813”>
    <student id = “21345589”>
  • Executable representation of Student object:
//Code Snippet: 2

Student s1 = new Student();
Student s2 = new Student();

The conversion of the XML representation of the objects created by code snippet 1 to the default executable Java representation of the objects created by code snippet 2 is called unmarshalling.

Unmarshaller in JAXB[edit]

The process of unmarshalling the XML data into an executable Java object is taken care of by the in-built Unmarshaller class. It also validates the XML data as it gets unmarshalled. The unmarshal methods defined in the Unmarshaller class are overloaded for the different types of XML inputs. Some of the important implementations of unmarshal methods:[3]

  • Unmarshalling from an XML File:
JAXBContext jcon = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller umar = jcon.createUnmarshaller();
Object obj = umar.unmarshal( new File( "input.xml" ) );
  • Unmarshalling from an XML file in InputStream:
InputStream istr = new FileInputStream( "input.xml" );
JAXBContext jcon = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller umar = jcon.createUnmarshaller();
Object obj = umar.unmarshal( istr );
  • Unmarshalling from an XML file in a URL:
JAXBContext jcon = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller umar = jcon.createUnmarshaller();
URL url = new URL( "http://merrilllynch.employee/input.xml" );
Object obj = umar.unmarshal( url );

Unmarshalling XML Data[edit]

Unmarshal methods can deserialize an entire XML document or a small part of it. When the XML root element is globally declared, these methods utilize the JAXBContext's mapping of XML root elements to JAXB mapped classes to initiate the unmarshalling. If the mappings are not sufficient and the root elements are declared locally, the unmarshal methods use declaredType methods for the unmarshalling process. These two approaches can be understood below.[3]

Unmarshal a global XML root element[edit]

The unmarshal method uses JAXBContext to unmarshal the XML data, when the root element is globally declared. The JAXBContext object always maintains a mapping of the globally declared XML element and its name to a JAXB mapped class. If the XML element name or its @xsi:type attribute matches the JAXB mapped class, the unmarshal method transforms the XML data using the appropriate JAXB mapped class. However, if the XML element name has no match, the unmarshal process will abort and throw an UnmarshalException. This can be avoided by using the unmarshal by declaredType methods.[4]

Unmarshal a local XML root element[edit]

When the root element is not declared globally, the application assists the unmarshaller by application-provided mapping using declaredType parameters. By an order of precedence, even if the root name has a mapping to an appropriate JAXB class, the declaredType overrides the mapping. However, if the @xsi:type attribute of the XML data has a mapping to an appropriate JAXB class, then this takes precedence over declaredType parameter. The unmarshal methods by declaredType parameters always return a JAXBElement<declaredType> instance. The properties of this JAXBElement instance are set as follows:[5]

JAXBElement Property Value
name xml element name
value instanceof declaredType
declaredType unmarshal method declaredType parameter
scope null (actual size is not known)

Comparison with deserialization[edit]

An object that is serialized is in the form of a byte stream and it can eventually be converted back to a copy of the original object. Deserialization is the process of converting the byte stream data back to its original object type.

An object that is marshalled, however, records the state of the original object and it contains the codebase (codebase here refers to a list of URLs where the object code can be loaded from, and not source code). Hence, in order to convert the object state and codebase(s), unmarshalling must be done. The unmarshaller interface automatically converts the marshalled data containing codebase(s) into an executable Java object in JAXB. Any object that can be deserialized can be unmarshalled. However, the converse need not be true.

See also[edit]


  1. ^ "Binding XML Schemas - The Java EE 5 Tutorial". docs.oracle.com. Retrieved 2016-09-14.
  2. ^ "Using the XmlSerializer Class". msdn.microsoft.com. Retrieved 2016-09-23.
  3. ^ a b "Unmarshaller (JAXB 2.2.3)". jaxb.java.net. Retrieved 2016-09-14.
  4. ^ "JAXBContext (JAXB 2.2.3)". jaxb.java.net. Retrieved 2016-09-23.
  5. ^ "JAXBElement (JAXB 2.2.3)". jaxb.java.net. Retrieved 2016-09-23.