JAXBと連携してJavaオブジェクトとXMLのマッピングをすることができます。
Marshaller, Unmarshallerという2つのインタフェースを使用して変換を行います。
例
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 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(); } } } ・・・ } |
<oxm:jaxb2-marshaller id="jaxb2marshaller" contextPath="foo.bar.schema"/> <bean id="application" class="Application"> <property name="marshaller" ref="jaxb2marshaller" /> <property name="unmarshaller" ref="jaxb2marshaller" /> </bean> |