JAXB - can class containment be flattened when sorting XML?

Let's say I have two classes:

@XmlRootElement
class A {
    @XmlElement
    String propertyOfA;
    @XmlElement
    B b;
}

class B {
    @XmlElement
    String propertyOfB;
} 

JAXB returns XML formatted as follows:

<a>
  <propertyOfA>valueA</propertyOfA>
  <b>
    <propertyOfB>valueB</propertyOfB>
  </b>
</a>

My question is how to smooth the hierarchy in XML? So that I have:

<a>
  <propertyOfA>valueA</propertyOfA>
  <propertyOfB>valueB</propertyOfB>
</a>

Can this be done with annotations?

Right now I'm going to create a wrapper class for A that constructs the fields the way I want to see them in XML. Is there a better way?

+5
source share
4 answers

Note. I am EclipseLink JAXB (MOXy) and a member of the JAXB 2 group (JSR-222) .

MOXy @XmlPath :

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

@XmlRootElement
class A {
    @XmlElement
    String propertyOfA;

    @XmlPath(".")
    B b;
}

+4

- , :

@XmlRootElement
class A {
    @XmlElement
    String propertyOfA;
    @XmlElement(name="propertyOfB")
    B b;
}
@XmlType(name="")
class B {
    @XmlValue
    String propertyOfB;
} 

: - . , .

+4

( JAXB) @XmlJavaTypeAdapter. , , . , A, . , , , , . , , - , , , .

0

All Articles