Part V. Data access - 21. Marshaller and Unmarshaller for marshalling XML-21.3 using O/X Mappers

Keywords: xml Spring Java PHP

Spring's OXM can be used in a variety of situations. In the following example, we will use it to marshal Spring-managed application settings as XML files. We will use a simple JavaBean to represent the settings:

public class Settings {

private boolean fooEnabled;

public boolean isFooEnabled() {
return fooEnabled;
}

public void setFooEnabled(boolean fooEnabled) {
this.fooEnabled = fooEnabled;
}
}

The application class uses this bean to store its settings. In addition to the main methods, this class has two ways: saveSettings () saves the settings bean to a file named settings.xml, and loadSettings () loads these settings again. A main () method constructs a Spring application context and calls both methods.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public class Application {

private static final String FILE_NAME = "settings.xml";
private Settings settings = new Settings();
private Marshaller marshaller;
private Unmarshaller unmarshaller;

public void setMarshaller(Marshaller marshaller) {
this.marshaller = marshaller;
}

public void setUnmarshaller(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}

public void saveSettings() throws IOException {
FileOutputStream os = null;
try {
os = new FileOutputStream(FILE_NAME);
this.marshaller.marshal(settings, new StreamResult(os));
} finally {
if (os != null) {
os.close();
}
}
}

public void loadSettings() throws IOException {
FileInputStream is = null;
try {
is = new FileInputStream(FILE_NAME);
this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));
} finally {
if (is != null) {
is.close();
}
}
}

public static void main(String[] args) throws IOException {
ApplicationContext appContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
Application application = (Application) appContext.getBean("application");
application.saveSettings();
application.loadSettings();
}
}

Application needs to set marshaller and unmarshaller attributes. We can use the following applicationContext.xml:

<beans>
<bean id="application" class="Application">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>
</beans>

The application context uses Castor, but we can use other marshaller instances described later in this chapter. Note that by default, Castor does not require any further configuration, so bean definitions are fairly simple. Also note that Castor Marshaller implements Marshaller and Unmarshaller, so we can refer to castor Marshaller beans in the application's marshaller and unmarshaller attributes.

This sample application generates the following settings.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<settings foo-enabled="false"/>

Posted by Takuma on Tue, 12 Feb 2019 06:57:19 -0800