Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlSeeAlso and XmlRootElement names?

Tags:

jaxb

In the reference JAXB implementation is there anyway to get XmlSeeAlso to use the name= value from XmlRootElement?

The effect I want is for the type attribute to use the name= value rather than actual class name from XmlSeeAlso.

Is this possible is some other JAXB implementation?

Small example:

@XmlRootElement(name="some_item")
public class SomeItem{...}

@XmlSeeAlso({SomeItem.class})
public class Resource {...}

XML:
<resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="some_item">
...
</resource>

Possible without a lot of effort?

like image 912
Mike Summers Avatar asked Sep 05 '12 19:09

Mike Summers


People also ask

What is @XmlSeeAlso?

Annotation Type XmlSeeAlsoInstructs JAXB to also bind other classes when binding this class. Java makes it impractical/impossible to list all sub-classes of a given class. This often gets in a way of JAXB users, as it JAXB cannot automatically list up the classes that need to be known to JAXBContext .

What is @XmlRootElement?

Annotation Type XmlRootElementMaps a class or an enum type to an XML element. Usage. The @XmlRootElement annotation can be used with the following program elements: a top level class. an enum type.

What is the use of @XmlElement?

A JavaBean property, when annotated with @XmlElement annotation is mapped to a local element in the XML Schema complex type to which the containing class is mapped. Example 2: Map a field to a nillable element. Example 3: Map a field to a nillable, required element.

What is @XmlRootElement in rest?

1. JAXB @XmlRootElement annotation type. @XmlRootElement maps a class or an enum type to an XML element. When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document.


1 Answers

About @XmlSeeAlso

The purpose of the @XmlSeeAlso annotation is just to let your JAXB (JSR-222) implementation know that when it is processing the metadata for Resource that it should also process the metadata for the SomeItem class. Some people mistakenly believe that it is related to mapping inheritance since that is the use case it is most often used with. Since the subclasses of a class can not be determined using Java reflection, @XmlSeeAlso is used to let the JAXB implementation know that mappings for the subclasses should also be created.


Below is an example of how you could support your use case:

Resource

The complex type name corresponding to a Java class is supplied via the @XmlType annotation.

package forum12288631;

import javax.xml.bind.annotation.XmlType;

@XmlType(name="some_item")
public class Resource {

}

Demo

The root element name can come from the @XmlRootElement annotation or can be supplied via an instance of JAXBElement. We will create an instance of JAXBElement and indicate that it is holding onto an instance of Object. When marshalled this will for the xsi:type attribute to be included in the output.

package forum12288631;

import javax.xml.bind.*;
import javax.xml.namespace.QName;

public class Demo {

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

        Resource resource = new Resource();
        JAXBElement<Object> jaxbElement = new JAXBElement<Object>(QName.valueOf("resource"), Object.class, resource);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, System.out);
    }

}

Output

The resulting XML has the root element supplied by the JAXBElement and the value of the xsi:type attribute comes from the @XmlType annotation on Resource.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="some_item"/>
like image 114
bdoughan Avatar answered Nov 23 '22 11:11

bdoughan