JAXB fragment with different namespaces

10,553

Without seeing your actual XML schema, files and JAXB generated classes (and their annotations) I can only advise you to try an adapt the following example to your scenario.

Given that you have a JAXB generated class like the following:

@XmlRootElement(namespace = "http://test.com")
@XmlType(namespace = "http://test.com")
public static final class Test {

  public String data;

  public Test() {}
}

which is in the package test and there is a package-info.java file in it like this:

@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED,
           xmlns = @XmlNs(prefix = "", namespaceURI = "http://test.com"))
package test;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

the following code:

Test test = new Test();
test.data = "Hello, World!";

JAXBContext context = JAXBContext.newInstance(Test.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(test, System.out);

will print this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test xmlns="http://test.com">
    <data>Hello, World!</data>
</test>

You can probably probably omit a the NamespacePrefixMapper implementation entirely.

Play with omitting particular namespace elements from any of the annotations and see how the output changes.

Blaise Doughan (a Java XML binding expert, probably lurking around somewhere near) has posted some info regarding this issue on his blog, see his post for some more insight.


Based on the input provided by Baptiste in chat I propose the following solution (I think this is the most painless).

Compile your schema files with XJC normally ($ xjc .). This will generate package-java.info files for each of the generated packages. I assume this schema isn't updated everyday, so you're safe to make modifications on the package-info.java files (even though there will be some line comments in those files telling you not to do it—do it anyway). If the schema gets updated and you have to re-compile it run XJC with the -npa switch, which tells it not to generate those package-info.java files automatically, so (ideally) you can't overwrite your hand-crafted files (if you use version control you can/should include these (handmade) files in the repository).

Based on the provided schema files four packages are generated, so I'll include my version of the modified package-info.java files.

@XmlSchema(namespace = "http://www.ebics.org/H000",
           xmlns = @XmlNs(prefix = "ns1", 
                          namespaceURI = "http://www.ebics.org/H000"))
package org.ebics.h000;

@XmlSchema(namespace = "http://www.ebics.org/H003",
           xmlns = @XmlNs(prefix = "", 
                          namespaceURI = "http://www.ebics.org/H003"))
package org.ebics.h003;

@XmlSchema(namespace = "http://www.ebics.org/S001",
           xmlns = @XmlNs(prefix = "ns3", 
                          namespaceURI = "http://www.ebics.org/S001"))
package org.ebics.s001;

@XmlSchema(namespace = "http://www.w3.org/2000/09/xmldsig#",
           xmlns = @XmlNs(prefix = "ns2", 
                          namespaceURI = "http://www.w3.org/2000/09/xmldsig#"))
package org.w3._2000._09.xmldsig;

After this you create your JAXBContext like this:

JAXBContext context =
  JAXBContext.newInstance("org.ebics.h003:org.ebics.s001:org.w3._2000._09.xmldsig");

(I've noticed that you don't use actually use the h000 package so I've omitted it from the package name list. If it is included, then the marshalled XML's root tag will probably contain its namespace and prefix mapping, even tough it isn't used.)

After this, you unmarshall your input XML and do whatever you want to with the object in memory.

Unmarshaller unmarshaller = context.createUnmarshaller();

EbicsNoPubKeyDigestsRequest ebicsNoPubKeyDigestsRequest =
    (EbicsNoPubKeyDigestsRequest) unmarshaller.unmarshal(stream);

Now, if you want to marshall only the header tag which is nested inside the ebicsNoPubKeyDigestsRequest you have to wrap it inside a JAXBElement<...> because the header's type EbicsNoPubKeyDigestsRequest.Header isn't annotated with the @XmlRootElement annotation. You have two (in this case one) way to create this element.

Create an ObjectFactory instance for the corresponding package and use its JAXBElement<T> createT(T t) function. Which wraps its input into a JAXBElement<...>. Unfortunately however, for the header field's type (given your schema files) XJC generates no such method, so you have to do it by hand.

Basically you've almost done it right, but when creating the JAXBElement<...> instead of passing it new QName("header") you have to create a fully-qualifed name, which implies that the namespace is specified too. Passing only the name of the XML tag isn't sufficient, because JAXB won't know this way that this particular header tag is part of the "http://www.ebics.org/H003" namespace. So do it like this:

QName qualifiedName = new QName("http://www.ebics.org/H003", "header");
JAXBElement<EbicsNoPubKeyDigestsRequest.Header> header =
    new JAXBElement<EbicsNoPubKeyDigestsRequest.Header>(
        qualifiedName, EbicsNoPubKeyDigestsRequest.Header.class, header);

I didn't test if changing only the QName instantiation solves your problem, but maybe it will. However I think it won't and you have to manually manage your prefixes to get a nice and consistent result.

Share:
10,553
Bastiflew
Author by

Bastiflew

Updated on June 04, 2022

Comments

  • Bastiflew
    Bastiflew almost 2 years

    I have to marshall a fragment of my root xml object :

    Header header = ebicsNoPubKeyDigestsRequest.getHeader();
    JAXBElement<org.ebics.h003.EbicsNoPubKeyDigestsRequest.Header> jaxbElement =
      new JAXBElement<EbicsNoPubKeyDigestsRequest.Header>(
        new QName("header"), EbicsNoPubKeyDigestsRequest.Header.class, header);
    byte[] headerXml = JAXBHelper.marshall(jaxbElement, true);
    

    but when I marshall ebicsNoPubKeyDigestsRequest the namespaces are not the same (in header fragment I have : xmlns:ns4="http://www.ebics.org/H003" but in ebicsNoPubKeyDigestsRequest I have xmlns="http://www.ebics.org/H003")

    If I marshall the header object directly, without using JAXBElement, I have an No @XmlRootElement error

    How I can have the same namespaces? NB : I already use a NamespacePrefixMapper class :

    marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
    
      @Override
      public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        if (namespaceUri.equals("http://www.ebics.org/H003")) {
          return "";
        } else if (namespaceUri.equals("http://www.w3.org/2000/09/xmldsig#")) {
          return "ds";
        } else if (namespaceUri.equals("http://www.ebics.org/S001")) {
          return "ns1";
        } else if (namespaceUri.equals("http://www.w3.org/2001/XMLSchema-instance")) {
          return "ns2";
        }
        return "";
      }
    });
    

    EDIT : here the different package-info.java :

    @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ebics.org/H003", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package org.ebics.h003;
    
    @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ebics.org/S001", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package org.ebics.s001;
    
    @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.w3.org/2000/09/xmldsig#", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package org.w3._2000._09.xmldsig_;
    
  • Bastiflew
    Bastiflew over 12 years
    Thanks but I must not modify generated classes from this xsd : ebics.org/fileadmin/unsecured/specification/schema_current/…‌​, but maybe I can change annotations with a xjb file ?
  • Kohányi Róbert
    Kohányi Róbert over 12 years
    @Baptiste I have only asked you to post your classes to see if they have any annotations at all (and yes, class file generation coulbe be manipulated via binding configurations). I'll generate the classes for myself and see what I can do. It would helpful I you could tell us how did you manage to compile these schema files with XJC too.
  • Bastiflew
    Bastiflew over 12 years
    Thanks a lot, I just begin with jaxb, I don't understand everything
  • ziggy
    ziggy about 12 years
    @KohányiRóbert Does this work the same when unmarshalling? i.e. XML to Java object?
  • Kohányi Róbert
    Kohányi Róbert about 12 years
    @ziggy I'm not exactly sure what you're referring to by work the same and I don't have my test project that I've used for my answer anymore ... but if you can marshall Java objects to an XML format similar to the one in the question/answer then I think you can unmarshall an XML like that to Java objects too. If this isn't working for some reason then I bet is because XJC didn't/couldn't properly generate Java classes from the supplied XSD (as I've mentioned in my answer). I hope it helped.