XmlSeeAlso and XmlRootElement?

Is there anyway in the JAXB reference implementation that XmlSeeAlso uses the name = value of XmlRootElement?

The effect I want is that the type attribute uses the value name = instead of the actual class name from XmlSeeAlso.

Is this possible in some other JAXB implementation?

A 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>

Perhaps without much effort?

+5
source share
1 answer

ABOUT @XmlSeeAlso

@XmlSeeAlso , JAXB (JSR-222) , Resource SomeItem. , , . Java, @XmlSeeAlso , JAXB , .


, :

, Java, @XmlType.

package forum12288631;

import javax.xml.bind.annotation.XmlType;

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

}

Demo

@XmlRootElement JAXBElement. JAXBElement , Object. xsi:type .

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);
    }

}

XML , JAXBElement, xsi:type @XmlType Resource.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="some_item"/>
+11

All Articles