Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:10:48+00:00 2026-05-27T19:10:48+00:00

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

  • 0

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_;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:10:49+00:00Added an answer on May 27, 2026 at 7:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Spring REST application that can marshall an object to JSON when a
I have something like: $client = new Zend_XmlRpc_Client('<XML-RPC URL>'); $result = $client->call('<method name>', array(
In WinForms, you have Control.BeginInvoke() , which means you can marshall a call from
I am using JAXB to marshall. I have noticed that when I am marshaling
I have this url: http://www.site.com/en/about.php?id=112&name=andrew marshall dickens and i would like to rewrite it
Suppose i have a xml file which has several nodes & children. I am
I have a build script fragment that looks as follows: foreach(...) ... add_custom_command( OUTPUT
I have a seriously annoying problem. My company uses castor to marshall and unmarshall
I have an object that uses a timer to occasionally poll for a resource
I have a fairly large repetitive XML to create using JAXB. Storing the whole

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.