Question

I have created Jaxb Class that contains following listener method but only unmarshall method is working :

void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
        System.out.println("afterUnmarshal  1");
    }

    void beforeMarshal(Marshaller marshaller, Object parent) {
        System.out.println("beforeMarshal  2");
    }

    void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
        System.out.println("beforeUnmarshal  3");
    }

    void afterMarshal(Marshaller marshaller, Object parent) {
        System.out.println("afterMarshal  4");
    }

OutPut :

beforeUnmarshal  3
afterUnmarshal  1

No marshall method is called on execution of marshalling code .


Updated Question : Problem : beforeMarshal Customer called two times . OutPut :

afterUnmarshal  Address
afterUnmarshal  Customer
beforeMarshal  Customer
beforeMarshal  Customer
beforeMarshal  Address

Program :

 public class Demo {

        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Customer.class,ObjectFactory.class);

            Unmarshaller u = jc.createUnmarshaller();
            File xml = new File("src/testRJE/input.xml");
            Customer customer = (Customer) u.unmarshal(xml);

            Marshaller m = jc.createMarshaller();
            m.marshal(customer, xml);
        }

    }


    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer {

      @XmlElementRef(name = "billing-address")
      @XmlJavaTypeAdapter(AddressAdapter.class)
      private Address address;

        public Address getAddress() {
            return address;
        }


        void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {//XML to Object
        System.out.println("afterUnmarshal  Customer");
    }

    void beforeMarshal(Marshaller marshaller ) {
        System.out.println("beforeMarshal  Customer");
    }

    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Address {

        public Address() {
        }


        private String street;

        private String city;

      void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
        System.out.println("afterUnmarshal  Address");
    }

    void beforeMarshal(Marshaller marshaller ) {
        System.out.println("beforeMarshal  Address");
    }


    }
Was it helpful?

Solution

The marshal event methods don't have an Object parameter like the unmarshal methods do. When your remove them from your method signatures everything should work as expected.

OTHER TIPS

Apparently you have extended the the class Unmarshaller#Listener which has only the methods afterUnmarshal and beforeUnmarshal which explains why only those two are called.

In order to Listen to marshaling events you need to have another Listener class extending Marshaller#Listener which in its turn has the methods you need and which you have to override.

Also please annotate methods you override with the annotation @Override. Having done that in your class you should have got errors saying that the class Unmarshaller#Listener does not have any methods beforeMarshal or afterMarshal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top