3

There are two JavaBean A and B. and 2 String C and D. There structure is

A
--B
----C
----D

I can marshall Object A to Xml

<A>
  <B>
   <C>ccc</C>
   <D>ddd</D>
  </B>
</A>

And In some cases, I'd like to marshall A to XML like this

<A>
   <C>ccc</C>
   <D>ddd</D>
</A>

Without B tag. can it be done by XMLAdapter, or other ways. it' the best if it has some dynamic ways. Thanks.

1 Answers1

1

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

You could leverage MOXy's @XmlPath extension for this use case:

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "A")
@XmlAccessorType(XmlAccessType.FIELD)
public class A {

    @XmlPath(".")
    private B b;

}

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Thanks, is the any ways that JAXB (JSR-222) supports? – user2211527 May 24 '13 at 09:15
  • I tried this approach and the example from your blog but still the `JAXB Marshalling` does not work as expected with `@XmlPath`. Seems like the XML produced with and without `@XmlPath` are same. Can you please once look into this example and provide your answer: https://stackoverflow.com/questions/67500565/xmlpath-has-no-impact-during-the-jaxb-marshalling – BATMAN_2008 May 12 '21 at 10:21