No @XmlRootElement generated by JAXB
Asked Answered
Z

17

249

I'm trying to generate Java classes from the FpML (Finanial Products Markup Language) version 4.5. A ton of code is generated, but I cannot use it. Trying to serialize a simple document I get this:

javax.xml.bind.MarshalException
  - with linked exception: [com.sun.istack.SAXException2: unable
  to marshal type
  "org.fpml._2008.fpml_4_5.PositionReport"
  as an element because it is missing an
  @XmlRootElement annotation]

In fact no classses have the @XmlRootElement annotation, so what can I be doing wrong?. I'm pointing xjc (JAXB 2.1) to fpml-main-4-5.xsd, which then includes all types.

Zoogeography answered 4/5, 2009 at 11:32 Comment(0)
I
296

To tie together what others have already stated or hinted at, the rules by which JAXB XJC decides whether or not to put the @XmlRootElement annotation on a generated class are non trivial (see this article).

@XmlRootElement exists because the JAXB runtime requires certain information in order to marshal/unmarshal a given object, specifically the XML element name and namespace. You can't just pass any old object to the Marshaller. @XmlRootElement provides this information.

The annotation is just a convenience, however - JAXB does not require it. The alternative to is to use JAXBElement wrapper objects, which provide the same information as @XmlRootElement, but in the form of an object, rather than an annotation.

However, JAXBElement objects are awkward to construct, since you need to know the XML element name and namespace, which business logic usually doesn't.

Thankfully, when XJC generates a class model, it also generates a class called ObjectFactory. This is partly there for backwards compatibility with JAXB v1, but it's also there as a place for XJC to put generated factory methods which create JAXBElement wrappers around your own objects. It handles the XML name and namespace for you, so you don't need to worry about it. You just need to look through the ObjectFactory methods (and for large schema, there can be hundreds of them) to find the one you need.

Indeterminism answered 31/1, 2010 at 19:15 Comment(9)
Special case solution: when you can modify the xsd used for class generation: After reading the link provided in this answer the solution in my case was to modify the xsd file used to generate the classes: I changed the definition of the root element to an inlined definition instead of using the reference to a type defined separetely. These allows JAXB to set this element as @XmlRootElement, which was not possible with the elementType which was used before for the root element.Nadene
<scowl> changing root element to be of inline type, however, makes all classes to be inner classes of the root type. Also, even if the root element type is defined AFTER the root element itself (apparently allowed by schema), JAXB will still not annotate with @XmlRootElement.Cortese
@PawelVeselov - The following will help you generate top level classes: blog.bdoughan.com/2011/07/jaxb-xjc-and-nested-classes.htmlKermitkermy
This example helped me to generate the XmlRootElementannotations I needed Configure Maven to generate classes from XML Schema using JAXBCatenane
i.e. new ObjectFactory().createPositionReport(positionReport) returns JAXBElement<PositionReport>Backside
What if the generated ObjectFactory method does not create a method that wraps the argument in a JXBElement? In my case, the factory method is 0-arity and just returns a new object. (Why are some classes given JAXBElement wrapper helpers and others not?) I guess in that case we must create the wrapper ourselves?Robbert
I've noticed this sometimes too @CarlG. Can anyone explain?Lucrecialucretia
@CarlG I'm in the same situation - no XmlRootElement nor JAXBElement appears in my classes. Have you found a solution for this case?Algesia
Using the ObjectFactory helped to solve my problem, thanks :)Hertzfeld
K
74

This is mentioned at the bottom of the blog post already linked above but this works like a treat for me:

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(new JAXBElement<MyClass>(new QName("uri","local"), MyClass.class, myClassInstance), System.out);
Kosher answered 3/5, 2011 at 13:18 Comment(3)
I prefer the marked answer, but this works for me too.Treaty
what is jc in the above snippet?Hoop
@ArunRaj it is the JAXBContext classKosher
L
62

As hinted at in one of the above answers, you won't get an XMLRootElement on your root element if in the XSD its type is defined as a named type, since that named type could be used elsewhere in your XSD. Try mking it an anonymous type, i.e. instead of:

<xsd:element name="myRootElement" type="MyRootElementType" />

<xsd:complexType name="MyRootElementType">
...
</xsd:complexType>

you would have:

<xsd:element name="myRootElement">
    <xsd:complexType>
    ...
    <xsd:complexType>
</xsd:element>
Lau answered 27/2, 2013 at 16:27 Comment(2)
That's not true for me. My type is anonymous (embedded inside my root element) and no XmlRootElement annotation is generated. Any idea?Algesia
this also helps in case you see your ObjectFactory generated methods without parametersPoussin
D
49

@XmlRootElement is not needed for unmarshalling - if one uses the 2 parameter form of Unmarshaller#unmarshall.

So, if instead of doing:

UserType user = (UserType) unmarshaller.unmarshal(new StringReader(responseString));

one should do:

JAXBElement<UserType> userElement = unmarshaller.unmarshal(someSource, UserType.class);
UserType user = userElement.getValue();

The latter code will not require @XmlRootElement annotation at UserType class level.

Dextran answered 9/8, 2011 at 15:22 Comment(3)
Do you know of an equally elegant way to marshal an object that doesn't have XmlRootElement - without wrapping it in a JAXBElement as mentioned by skaffman, Gurnard et al?Highstrung
+1 Works perfectly! One edit for more clarity... In your solution 'someSource' is very vague term. To elaborate : JAXBElement<TargetClazz> root = unmarshaller.unmarshal(new StreamSource(new File("some.xml")),TargetClazz.class);Germainegerman
Further elaboration of 'someSource':String pathname = "file.xml"; InputStream stream = new FileInputStream(pathname); JAXBContext jaxbContext = JAXBContext.newInstance(UserType.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLEventReader someSource = factory.createXMLEventReader(stream); JAXBElement<UserType> userElement = jaxbUnmarshaller.unmarshal(someSource, UserType.class); UserType user = userElement.getValue();Eugenol
O
25

You can fix this issue using the binding from How to generate @XmlRootElement Classes for Base Types in XSD?.

Here is an example with Maven

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                <packageName>com.mycompany.schemas</packageName>
                <bindingFiles>bindings.xjb</bindingFiles>
                <extension>true</extension>
            </configuration>
        </plugin>

Here is the binding.xjb file content

<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
              jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings schemaLocation="path/to/myschema.xsd" node="/xs:schema">
        <jxb:globalBindings>
            <xjc:simple/>
        </jxb:globalBindings>
    </jxb:bindings>
</jxb:bindings>
Oleson answered 16/5, 2012 at 7:13 Comment(1)
Indeed, using <xjc:simple> in the binding.xjb file did the trick. Awesome solution if you don't want to change your marshaling code or your WSDL. Note that xjc:simple generates different method names (plural) for collection getters (getOrders instead of getOrder for example)Drongo
M
21

Joe's answer (Joe Jun 26 '09 at 17:26) does it for me. The simple answer is that absence of an @XmlRootElement annotation is no problem if you marshal a JAXBElement. The thing that confused me is the generated ObjectFactory has 2 createMyRootElement methods - the first takes no parameters and gives the unwrapped object, the second takes the unwrapped object and returns it wrapped in a JAXBElement, and marshalling that JAXBElement works fine. Here's the basic code I used (I'm new to this, so apologies if the code's not formatted correctly in this reply), largely cribbed from link text:

ObjectFactory objFactory = new ObjectFactory();
MyRootElement root = objFactory.createMyRootElement();
...
// Set root properties
...
if (!writeDocument(objFactory.createMyRootElement(root), output)) {
    System.err.println("Failed to marshal XML document");
}
...

private boolean writeDocument(JAXBElement document, OutputStream output) {

  Class<?> clazz = document.getValue().getClass();
  try {
    JAXBContext context =
        JAXBContext.newInstance(clazz.getPackage().getName());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(document, output);
    return true;

  } catch (JAXBException e) {
    e.printStackTrace(System.err);
    return false;
  }
}
Mackintosh answered 30/12, 2010 at 15:58 Comment(1)
I have a case where my ObjectFactory class only defines methods that return regular instances and not JAXBElement instances...Algesia
G
12

As you know the answer is to use the ObjectFactory(). Here is a sample of the code that worked for me :)

ObjectFactory myRootFactory = new ObjectFactory();

MyRootType myRootType = myRootFactory.createMyRootType();

try {

        File file = new File("./file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(MyRoot.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        //output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        JABXElement<MyRootType> myRootElement = myRootFactory.createMyRoot(myRootType);

        jaxbMarshaller.marshal(myRootElement, file);
        jaxbMarshaller.marshal(myRootElement, System.out);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
Gentes answered 18/2, 2016 at 18:15 Comment(2)
to your point... how do I use JAXBElement<?> create...() methods from ObjectFactory for nested elements? i.e. : <SOAP-ENV:Header> <wsse:Security> <wsse:UsernameToken></wsse:UsernameToken> </wsse:Security> </SOAP-ENV:Header> I get: "unable to marshal type "UsernameTokenType" as an element because it is missing an @XmlRootElement annotation"Leu
indeed this pretty clear code expample did the trick for me. Thanks for saving me from deep-diving into library-code :)Margarine
B
10

After sruggling for two days I found the solution for the problem.You can use the ObjectFactory class to workaround for the classes which doesn't have the @XmlRootElement. ObjectFactory has overloaded methods to wrap it around the JAXBElement.

Method:1 does the simple creation of the object.

Method:2 will wrap the object with @JAXBElement.

Always use Method:2 to avoid javax.xml.bind.MarshalException - with linked exception missing an @XmlRootElement annotation.

Please find the sample code below

Method:1 does the simple creation of the object

public GetCountry createGetCountry() {
        return new GetCountry();
    }

Method:2 will wrap the object with @JAXBElement.

 @XmlElementDecl(namespace = "my/name/space", name = "getCountry")
 public JAXBElement<GetCountry> createGetCountry(GetCountry value) {
        return new JAXBElement<GetCountry>(_GetCountry_QNAME, GetCountry.class, null, value);
    }

Working code sample:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
WebServiceTemplate springWSTemplate = context.getBean(WebServiceTemplate.class);

GetCountry request = new GetCountry();
request.setGuid("test_guid");

JAXBElement<GetCountryResponse> jaxbResponse = (JAXBElement<GetCountryResponse>)springWSTemplate .marshalSendAndReceive(new ObjectFactory().createGetCountry(request));

GetCountryResponse response = jaxbResponse.getValue();
Boyd answered 3/3, 2018 at 12:1 Comment(1)
Thanks for giving the code reference with spring webservice template as was struggling to figure it out for quite some time!Marinetti
J
8

JAXBElement wrappers works for cases where no @XmlRootElement is generated by JAXB. These wrappers are available in ObjectFactory class generated by maven-jaxb2-plugin. For eg:

     public class HelloWorldEndpoint {
        @PayloadRoot(namespace = NAMESPACE_URI, localPart = "person")
        @ResponsePayload
        public JAXBElement<Greeting> sayHello(@RequestPayload JAXBElement<Person> request) {

        Person person = request.getValue();

        String greeting = "Hello " + person.getFirstName() + " " + person.getLastName() + "!";

        Greeting greet = new Greeting();
        greet.setGreeting(greeting);

        ObjectFactory factory = new ObjectFactory();
        JAXBElement<Greeting> response = factory.createGreeting(greet);
        return response;
      }
 }
Jimmie answered 9/8, 2017 at 15:31 Comment(0)
Z
7

In case my experience of this problem gives someone a Eureka! moment.. I'll add the following:

I was also getting this problem, when using an xsd file that I had generated using IntelliJ's "Generate xsd from Instance document" menu option.

When I accepted all the defaults of this tool, it generated an xsd file that when used with jaxb, generated java files with no @XmlRootElement. At runtime when I tried to marshal I got the same exception as discussed in this question.

I went back to the IntellJ tool, and saw the default option in the "Desgin Type" drop down (which of course I didn't understand.. and still don't if I'm honest) was:

Desgin Type:

"local elements/Global complex types"

I changed this to

"local elements/types"

, now it generated a (substantially) different xsd, that produced the @XmlRootElement when used with jaxb. Can't say I understand the in's and out's of it, but it worked for me.

Zenia answered 26/11, 2014 at 15:44 Comment(0)
C
6

It's not working for us either. But we did find a widely-quoted article that adds SOME background... I'll link to it here for the sake of the next person: https://web.archive.org/web/20070812073809/http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html

Carmeliacarmelina answered 18/6, 2009 at 15:38 Comment(3)
This worked well for me, thank you. I also found that I was marshaling the wrong JAXB object (not the root like I thought) in the process of going through this. I forgot to create a JAXBElement and was trying to marshal just the returned object from the ObjectFactory class I had obtained from binding. This basically took care of the issue altogether (in case anyone else runs up against the same problem).Tellford
404 : "We're sorry the java.net site has closed. Most Open Source projects previously hosted on java.net have been relocated."Twum
web.archive.org/web/20070812073809/http://weblogs.java.net/blog/…Dietsche
S
6

With a Maven build, you can add the @XmlRootElement annotation

with the "jaxb2-basics-annotate" plug-in.

See more information : see

Configure Maven to generate classes from XML Schema using JAXB

and JAXB XJC code generation

Suribachi answered 7/6, 2012 at 10:13 Comment(0)
C
6

The topic is quite old but still relevant in enterprise business contexts. I tried to avoid to touch the xsds in order to easily update them in the future. Here are my solutions..

1. Mostly xjc:simple is sufficient

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jxb:bindings version="2.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jxb:extensionBindingPrefixes="xjc">

    <jxb:globalBindings>
        <xjc:simple/> <!-- adds @XmlRootElement annotations -->
    </jxb:globalBindings>

</jxb:bindings>

It will mostly create XmlRootElements for importing xsd definitions.

2. Divide your jaxb2-maven-plugin executions

I have encountered that it makes a huge difference if you try to generate classes from multiple xsd definitions instead of a execution definition per xsd.

So if you have a definition with multiple <source>'s, than just try to split them:

          <execution>
            <id>xjc-schema-1</id>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <xjbSources>
                <xjbSource>src/main/resources/xsd/binding.xjb</xjbSource>
              </xjbSources>
              <sources>
                <source>src/main/resources/xsd/definition1/</source>
              </sources>
              <clearOutputDir>false</clearOutputDir>
            </configuration>
          </execution>

          <execution>
            <id>xjc-schema-2</id>
            <goals>
              <goal>xjc</goal>
            </goals>
            <configuration>
              <xjbSources>
                <xjbSource>src/main/resources/xsd/binding.xjb</xjbSource>
              </xjbSources>
              <sources>
                <source>src/main/resources/xsd/definition2/</source>
              </sources>
              <clearOutputDir>false</clearOutputDir>
            </configuration>
          </execution>

The generator will not catch the fact that one class might be sufficient and therefore create custom classes per execution. And thats exactly what I need ;).

Curriculum answered 30/3, 2020 at 7:25 Comment(2)
where that .xjb-file should be added - server or client? how to make it visible to the project? thanksHenriques
@AndreyM.Stepanov what do you mean by "server or client"? You need to use them within the project which generates the java classes (only at build time)Curriculum
C
5

So I was using mavens maven-jaxb2-plugin to generate the classes from a large & complicated WSDL file and run into this issue. The problem being that elements in the WSDL referenced complexType definitions as the type and therefore the element classes where not generated and when trying to use the complexType classes it produced the missing @XmlRootElement error.

Modifying the WSDL is not really a viable solution in my oppinion, the only thing practical seemed to engineer a way to add the missing annotation during generation. It also caused problems with the serialization when marshalling because the request was sending the wrong element name and there was no class with a matching element name for the response either.

What I ended up doing was using a second maven plugin jaxb2-basics-annotate which allows you to add the missing annotations to the required classes through the use of a jaxb binding file. This saves you having to sort this problem out without adding unnecessary code and also means you can re-generate easily if you need to use an updated WSDL file in the future.

pom.xml (Note that there is a plugin section in the configuration for the execution - The WSDL file location is /src/main/resources/wsdl/EstimatingService.wsdl)

<project>
    ...
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <jaxb2.version>0.14.0</jaxb2.version>
        <jaxb2.annotate.version>1.1.0</jaxb2.annotate.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>${jaxb2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-annotate</artifactId>
            <version>${jaxb2.annotate.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>${jaxb2.version}</version>
                <executions>
                    <execution>
                        <id>estimating</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <schemaLanguage>WSDL</schemaLanguage>
                            <generateDirectory>target/generated-sources/acme/src/gen/estimating-service</generateDirectory>
                            <generatePackage>com.acme.shipping.estimating.service</generatePackage>
                            <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                            <schemaIncludes>
                                <include>EstimatingService.wsdl</include>
                            </schemaIncludes>
                            <bindingDirectory>${project.basedir}/src/main/resources/bindings</bindingDirectory>
                            <bindingIncludes>estimateServiceBinding.xjb</bindingIncludes>
                            <extension>true</extension>
                            <args>
                                <arg>-Xannotate</arg>
                                <arg>-XremoveAnnotation</arg>
                            </args>
                            <plugins>
                                <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                    <artifactId>jaxb2-basics-annotate</artifactId>
                                </plugin>
                            </plugins>
                        </configuration>
                    </execution>
                    ...
                    // More executions here if you have multiple WSDL files (Dont forget to give it a different package name and id)
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
</project>  

estimateServiceBinding.xjb (jaxb binding file used in this example - /src/main/resources/bindings/estimateServiceBinding.xjb)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:annox="http://annox.dev.java.net" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <jaxb:globalBindings generateElementProperty="false">
        <xjc:simple />
    </jaxb:globalBindings>
    <!-- Target the schema section in the WSDL file using the given target namespace which contains the complexType definitions we want to annotate -->
    <jaxb:bindings schemaLocation="../wsdl/EstimatingService.wsdl" node="//xs:schema[@targetNamespace='http://acme.com/schema/datatypes/v2']">
        <jaxb:bindings node="xs:complexType[@name='GetQuickEstimateRequestContainer']">
            <!-- Add the @XmlRootElement annotation to the generated class and then tell it use the correct element name required when marshalling. e.g GetQuickEstimateRequestContainer element is renamed to the element name that referenced it in the WSDL (GetQuickEstimateRequest) -->
            <annox:annotateClass>@javax.xml.bind.annotation.XmlRootElement(name="GetQuickEstimateRequest")</annox:annotateClass>
        </jaxb:bindings>
        <jaxb:bindings node="xs:complexType[@name='GetQuickEstimateResponseContainer']">
            <annox:annotateClass>@javax.xml.bind.annotation.XmlRootElement(name="GetQuickEstimateResponse")</annox:annotateClass>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

Generated class (GetQuickEstimateRequestContainer.java) with @XmlRootElement annotation and correct element name

@XmlRootElement(name = "GetQuickEstimateRequest")
public class GetQuickEstimateRequestContainer {
    ...
    // class member fields & setters and getters
    ...
}
Centralize answered 26/8, 2021 at 23:46 Comment(0)
F
4

Did you try to change your xsd like this?

<!-- create-logical-system -->
<xs:element name="methodCall">
  <xs:complexType>
    ...
  </xs:complexType>
</xs:element>
Frohman answered 2/5, 2011 at 22:40 Comment(1)
This worked for me with JDK 1.7u71. A top level element gets assigned the @XmlRootElement by xjc. Initially I had a top level complex type only. Having to wrap in a JAXBElement is plain ugly.Pippo
P
1

To soluction it you should configure a xml binding before to compile with wsimport, setting generateElementProperty as false.

     <jaxws:bindings wsdlLocation="LOCATION_OF_WSDL"
      xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
      xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
         <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
    <jaxws:bindings  node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='NAMESPACE_OF_WSDL']">
      <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xjc:generateElementProperty>false</xjc:generateElementProperty> 
      </jxb:globalBindings>
  </jaxws:bindings>
</jaxws:bindings>
Parsnip answered 15/11, 2011 at 15:14 Comment(1)
the wrapping tag should be <jaxb:bindings> ... <jaxws:bindings> ... </jaxws:bindings> ... </jaxb:bindings>Allrud
U
0

I just was struggling for a while with the same problem and just want to post my final result which works fine for me. So the base problems have been:

  • I have to generate xml strings from JAXB class instances with have no XmlRootElement annotations
  • The classes need additional classes to be bound for the marshalling process

The following class works fine for this problem:

public class Object2XmlConverter {

    public static <T> String convertToString(final T jaxbInstance, final Class<?>... additionalClasses)
            throws JAXBException {
        final Class<T> clazz = (Class<T>) jaxbInstance.getClass();

        final JAXBContext jaxbContext;
        if (additionalClasses.length > 0) {
            // this path is only necessary if you need additional classes to be bound
            jaxbContext = JAXBContext.newInstance(addClassesToBeBound(clazz, additionalClasses));
        } else {
            jaxbContext = JAXBContext.newInstance(clazz);
        }

        final QName qname = new QName("", jaxbInstance.getClass().getSimpleName());
        final JAXBElement<T> jaxbElement = new JAXBElement<T>(qname, clazz, null, jaxbInstance);

        final Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        final StringWriter stringWriter = new StringWriter();
        jaxbMarshaller.marshal(jaxbElement, stringWriter);
        return stringWriter.toString();
    }

    private static <T> Class<?>[] addClassesToBeBound(final Class<T> clazz, final Class<?>[] additionalClasses) {
        final Class<?>[] classArray = new Class<?>[additionalClasses.length + 1];
        for (int i = 0; i < additionalClasses.length; i++) {
            classArray[i] = additionalClasses[i];
        }
        classArray[classArray.length - 1] = clazz;
        return classArray;
    }

    public static void main(final String[] args) throws Exception {
        final Ns1TargetHeaderTyp dataTyp = ...;
        System.out.println(convertToString(dataTyp));
    }
}
Unwarrantable answered 30/11, 2020 at 18:51 Comment(1)
how to dynamically extract namespace from package-info?Annam

© 2022 - 2024 — McMap. All rights reserved.