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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:33:02+00:00 2026-05-22T11:33:02+00:00

being a newbie to Java XML binding i am facing a challenge. Let say

  • 0

being a newbie to Java XML binding i am facing a challenge.

Let say i have a scenario where my domain model is constructed and i want to marshall this domain to an xml structure.

Now i want to provide different unmarshall path’s:

  1. Marshall the whole object graph [no problem here]
  2. Marshall an objectgraph until a specific depth!!! [challenge]

I cannot figure out a good way on how to tackle this without introducing to much complexity. One can make a copy of the domain and manually later that, but that does not feel right. Any other solutions available?

  • 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-22T11:33:03+00:00Added an answer on May 22, 2026 at 11:33 am

    You could leverage XmlAdapter and Marshal.Listener to get this behaviour:

    Demo

    A Marshal.Listener will be set to keep track of the depth of the tree we are marshalling. Also we will set runtime level XmlAdapters that are aware of the depth listener. These adapters will start returning null when the desired depth has been reached.

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Root rootA = new Root();
            rootA.setName("A");
    
            Root rootB = new Root();
            rootB.setName("B");
            rootA.setChild(rootB);
    
            Root rootC = new Root();
            rootC.setName("C");
            rootB.setChild(rootC);
    
            Root rootD = new Root();
            rootD.setName("D");
            rootC.setChild(rootD);
    
            Root rootE = new Root();
            rootE.setName("E");
            rootD.setChild(rootE);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            DepthListener depthListener = new DepthListener(3);
            marshaller.setListener(depthListener);
            marshaller.setAdapter(new RootAdapter(depthListener));
            marshaller.marshal(rootA, System.out);
        }
    
    }
    

    DepthListener

    The purpose of this class is to to keep track of the current depth.

    import javax.xml.bind.Marshaller;
    
    public class DepthListener extends Marshaller.Listener {
    
        private int targetDepth;
        private int currentDepth = 0;
    
        public DepthListener(int depth) {
            this.targetDepth = depth;
        }
    
        @Override
        public void beforeMarshal(Object source) {
            currentDepth++;
        }
    
        @Override
        public void afterMarshal(Object source) {
            currentDepth--;
        }
    
        public boolean isMarshalDepth() {
            return currentDepth <= targetDepth; 
        }
    
    }
    

    RootAdapter

    The purpose of the XmlAdapter is to start returning null when the desired depth has been reached to stop the marshalling process.

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class RootAdapter extends XmlAdapter<Root, Root> {
    
        private DepthListener depthListener;
    
        public RootAdapter() {
        }
    
        public RootAdapter(DepthListener depthListener) {
            this.depthListener = depthListener;
        }
    
        @Override
        public Root unmarshal(Root root) throws Exception {
            return root;
        }
    
        @Override
        public Root marshal(Root root) throws Exception {
            if(depthListener != null && !depthListener.isMarshalDepth()) {
                return null;
            }
            return root;
        }
    
    }
    

    Root

    The following demonstrates how to specify the XmlAdapter on the domain object via the @XmlJavaTypeAdapter annotation:

    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    @XmlJavaTypeAdapter(RootAdapter.class)
    @XmlType(propOrder={"name", "child"})
    public class Root {
    
        private String name;
        private Root child;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Root getChild() {
            return child;
        }
    
        public void setChild(Root report) {
            this.child = report;
        }
    
    }
    

    Output

    The following is the output from the demo code:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root>
        <name>A</name>
        <child>
            <name>B</name>
            <child>
                <name>C</name>
            </child>
        </child>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As being quite a newbie in linux, I have the follwing question. I have
Being a newbie I have read forums and posts but still can't work out
Very primitive question but I am stuck (I guess being newbie). I have a
I have being studying (newbie) .NET and I got some doubts. Reading from a
I have no idea where to start debugging being a newbie. I got the
As someone who came from Java background and being a newbie to Ruby, I
Being the 'newbie' I have been staring at the 'documentation and api reference' docs
Java Newbie question : I need to capture the text being written to a
Being stuck with a legacy database schema that no longer reflects your data model
Being vaguely familiar with the Java world I was googling for a static analysis

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.