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 8566369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:38:23+00:00 2026-06-11T17:38:23+00:00

I have one sub-tree that I would like to append on an object and

  • 0

I have one sub-tree that I would like to append on an object and make JAXB marshall the all thing as a single tree (and with appropriate tags). But currently, the root tag of the sub-tree is replaced by the tag of another object

Unfortunately, I am not allowed to publish the original code here, so I reproduced my problem in a test code (so bear with me if you find this dumb).

The idea is that I would like to output the following structure:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Root xmlns:ns2="urn:my:foo:bar:1.0" xmlns:ns3="urn:other:foo:bar:1.1">
    <Content>
        <Header>
            <ns3:Leaf/>
        </Header>
    </Content>
</ns2:Root>

but currently, all I get is this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Root xmlns:ns2="urn:my:foo:bar:1.0" xmlns:ns3="urn:other:foo:bar:1.1">
    <Content>
        <Header/>
    </Content>
</ns2:Root>

I have two XSD’s to generate all the necessary classes, so I am ok on that side (but since those classes are generated, I cannot modify them).

Here is a sample code that produces the second XML (the wrong one):

package foo.bar;

import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Test {

    private JAXBContext context;

    public Test() throws JAXBException {
        context = JAXBContext.newInstance(RootElement.class, LeafElement.class);
    }

    @XmlRootElement(name = "Root", namespace = "urn:my:foo:bar:1.0")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Root", propOrder = { "content" })
    public static class RootElement {
        @XmlElement(name = "Content")
        protected ContentElement content;

        public ContentElement getContent() {
            return content;
        }

        public void setContent(ContentElement content) {
            this.content = content;
        }
    }

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Content", propOrder = { "dummy" })
    public static class ContentElement {
        @XmlElement(name = "Header")
        protected Object dummy;

        public Object getDummy() {
            return dummy;
        }

        public void setDummy(Object dummy) {
            this.dummy = dummy;
        }
    }

    @XmlRootElement(name = "Leaf", namespace = "urn:other:foo:bar:1.1")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Leaf")
    public static class LeafElement {

    }

    public Node marshal(Object obj) throws JAXBException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document doc = null;
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.newDocument();
        } catch (ParserConfigurationException ex) {
            throw new JAXBException(ex);
        }

        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, doc);
        return doc.getDocumentElement();
    }

    public void marshal(Object obj, OutputStream stream) throws JAXBException {
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, stream);
    }

    public void test() throws JAXBException {
        RootElement root = new RootElement();
        ContentElement content = new ContentElement();
        root.setContent(content);
        LeafElement leaf = new LeafElement();
        content.setDummy(marshal(leaf));
        marshal(root, System.out);
    }

    public static void main(String[] args) throws JAXBException {
        new Test().test();
    }

}

In that code you find 3 “marshallable” classes:

  1. RootElement,
  2. ContentElement and
  3. LeafElement.

The first two classes come from one XSD (with a given namespace) and the last one comes from another XSD (with another namespace), as illustrated in the sample code.

So far, all I found to fix this, was to create an additional class that would be set as dummy on the ContentElement and would itself hold the LeafElement, so that JAXB creates the appropriate intermdiate Node. But I find this solution quite ugly, not really maintainable and was hoping that JAXB had some way to handle such cases.

If you need more info, or you need me to re-formulate my question, don’t hesitate. I am having a hard time to explain my problem with simple words.

Constraints are the following:

  • I cannot modify RootElement, ContentElement nor LeafElement
  • I cannot use something else than JAXB
  • 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-06-11T17:38:24+00:00Added an answer on June 11, 2026 at 5:38 pm

    If you cannot change any element class, then you must create a holder object to leaf.

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class LeafElementHolder {
        @XmlAnyElement
        private Object leaf;
    
        public Object getLeaf() {
            return leaf;
        }
    
        public void setLeaf(Object leaf) {
            this.leaf = leaf;
        }
    }
    

    Add this class to context

    public Test() throws JAXBException {
        context = JAXBContext.newInstance(RootElement.class, LeafElement.class, LeafElementHolder.class);
    }
    

    and use this in your test() method

        LeafElement leaf = new LeafElement();
        LeafElementHolder holder = new LeafElementHolder();
        holder.setLeaf(leaf);
        content.setDummy(marshal(holder));
    

    You have 4 elements in XML so you must have 4 classes in Java.

    ns2:Root
      ns2:Content
        ns2:Header
          ns3:Leaf
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one master report with sub report. in master report display single value
I have a sub query that returns one column, showing as GroupType, I then
have one time consuming step that flattens a bunch of files. basically i'd like
I am going to have one directory with multiple sub-directories. I want to be
I have a multi-dimensional array, which basically consists of one sub-array for each year.
i have a problem to distributing application with 2 project (one project with sub-project
I have one user (only one, all the others are fine) trying to update
I have a directory tree that I need to process as follows: I have
I'd like to use namespaces as one would in javascript by using the keyword
I have a master project that's using a fairly standard source tree approach +

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.