package com.test; import java.io.StringWriter; import java.net.URL; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.xml.Marshaller; import com.test.model.Party; import com.test.model.Person; public class MarshallerTest2 { public static void main (String[] args) { Party party = getParty(); try { Mapping map = new Mapping(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); URL mappingFileURL = classLoader.getResource("Mapping.xml"); map.loadMapping(mappingFileURL); Marshaller marshaller = new Marshaller(); marshaller.setMapping(map); marshaller.setNamespaceMapping("", "http://ACORD.org/Standards/Life/2"); marshaller.setSuppressNamespaces(true); StringWriter writer = new StringWriter(); marshaller.setWriter(writer); marshaller.marshal(party); System.out.println(writer.toString()); } catch (Exception e) { e.printStackTrace(); } } private static Party getParty() { Party party = new Party(); party.setId("PaidProducerGUID1"); party.setPartyTypeCode("def"); // not setting to null this time party.setPartyTypeCodeCode("1"); party.setGovtID("000000000"); party.setGovtIDTC("abc"); // not setting to null this time party.setGovtIDTCCode("11"); Person person = new Person(); person.setFirstName("Andy"); person.setMiddleName("S"); person.setLastName("Roddick"); person.setPrefix("Mr."); party.setPerson(person); return party; } }