Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] OXM and combining xml elements

Hi Julius,

You could set up an object model like the following to handle your XML message:

Root

You will need an object to correspond to the repeating XML element called "Element".

import java.util.List;
import javax.xml.bind.annotation.*;

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

    @XmlElementWrapper(name="ListOfElements")
    @XmlElement(name="Element")
    private List<Element> listOfElements;

}


Element

The Element class will hold onto 2 properties one of type ElementOfTypeA and another of ElementOfTypeB.  Since JAXB by default won't marshal a null value, you will get the output you are looking for.

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Element {

    @XmlElement(name="ElementOfTypeA")
    private ElementOfTypeA elementOfTypeA;
   
    @XmlElement(name="ElementOfTypeB")
    private ElementOfTypeB elementOfTypeB;

}


-Blaise

On 13-10-18 7:44 AM, Julius Schwartzenberg wrote:
Hi,

I have a need to produce XML according to the following format:

<root>
  <ListOfElements>
    <Element>
      <ElementOfTypeA>
      </ElementOfTypeA>
    </Element>
    <Element>
      <ElementOfTypeA>
      </ElementOfTypeA>
    </Element>
    <Element>
      <ElementOfTypeB>
      </ElementOfTypeB>
    </Element>
    <Element>
      <ElementOfTypeB>
      </ElementOfTypeB>
    </Element>
  </ListOfElements>
</root>

ElementOfTypeA and ElementOfTypeB are based on different Java classes. The extra 'Element' elements do not make any sense, but I have to generate them like that.

I tried multiple combinations where I either provided ElementOfTypeA and ElementOfTypeB from my Java model through different List attributes or with a combined list using a superclass.

In the first case, I used these formats:
<xml-elements java-attribute="listWithElementsOfTypeA">
<xml-element-wrapper name="ListOfElements/Element/" />
</xml-elements>

and:
<xml-element java-attribute="listWithElementsOfTypeA"
                                          name="ElementOfTypeA">
  <xml-element-wrapper name="ListOfElements/Element" />
</xml-element>

and also:
<xml-element java-attribute="listWithElementsOfTypeA"
                                  name="Element/ElementOfTypeA">
  <xml-element-wrapper name="ListOfElements" />
</xml-element>

But MOXy combines the 'Element' elements and I end up with this:
<root>
  <ListOfElements>
    <Element>
      <ElementOfTypeA>
      </ElementOfTypeA>
      <ElementOfTypeA>
      </ElementOfTypeA>
      <ElementOfTypeB>
      </ElementOfTypeB>
      <ElementOfTypeB>
      </ElementOfTypeB>
    </Element>
  </ListOfElements>
</root>

In the second case, MOXy added an xsi-type attribute to the Element elements. I used this then:
<xml-elements java-attribute="elementKoppelingen">
  <xml-element type=ElementOfTypeA name=Element />
  <xml-element type=ElementOfTypeB name=Element />
  <xml-element-wrapper name="ListOfElements" />
</xml-elements>

How can I prevent MOXy from combining my Element elements?

Thanks a lot in advance,
Julius
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top