0

I got an xsd and I generated JaxB-Classes from it (example ApplicationCustomType). Some classes have an xs:any as element. I can add content (example xs:any) to these fields. The marshalling works well.

But when I try to unmarshall it FullContent contentType = XmlObjectHelper.getXmlTypeFromString(contentType, FullContent.class); The JaxB-classes are not populated with the fields from every xs:any. All other fields filled in as they should but the binding of the xs:any seems not to work.

I read that serializing-with-jaxb answer and it looks pretty the same I don't think I forgot something. I also tried to add lax = true but again it did not unmarshal my xml.

What did I do wrong or what did I forget?

public class ApplicationCustomType {

@XmlAnyElement
protected List<Element> any;
@XmlAnyAttribute
private Map<QName, String> otherAttributes = new HashMap<QName, String>();

An example xs:any element.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addressAttachment", propOrder = {
    "locale",
    "ourUserId"

})

public class AddressAttachment {

    @XmlElement(required = false)
    protected String locale;

    @XmlElement(required = false)
    protected String ourUserId;
}

ObjectFactory.java

@XmlElementDecl(namespace = "http://***/xsd/addressAttachment/v1", name = "information")
public JAXBElement<AddressAttachment> createAddressAttachment(AddressAttachment value) {
    return new JAXBElement<AddressAttachment>(_AddressAttachment_QNAME, AddressAttachment.class, null, value);
}

The xml I receive:

<content>
   <applicationCustom>
       <addressAttachment>
          <locale>CH.de</locale>
          <ourUserId>264646337383839</ourUserId>                       
       </addressAttachment>
   </applicationCustom>
</content>

Solution:

We retried it with (lax = true) on top of the List and not the class.

public class ApplicationCustomType {

@XmlAnyElement(lax = true)
protected List<Element> any;

If you use <Element> or <Object> does not affect the result. When using Element you dont have to provide the class to the marshaller actually. But in both cases you need to add @XmlElementDecl for each XMLRootclass in your Object factory for each Object you want to add to the xs:any list.

Community
  • 1
  • 1
SWiggels
  • 2,159
  • 1
  • 21
  • 35

1 Answers1

1

try to use it like this:

@XmlAnyElement
protected List<Object> any;

And look what is inside!

Explanation is here: https://dzone.com/articles/jaxbs-xmlanyelementlaxtrue

You should also supply known classes(AddressAttachment.class...) to the unmarshaller, otherwise it can't find out what is what in the xml right?

Not shown in your example but should look like this:

JAXBContext.newInstance(FullContent.class,AddressAttachment.class ...);

With Any there is no reference in the root object itself.

Have a nice time with Jaxb

Daniel Kec
  • 529
  • 2
  • 8
  • I have added your suggestion but I still have a null as you can see in the Links for Code and Result. – SWiggels Sep 04 '15 at 06:32