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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:09:31+00:00 2026-06-09T01:09:31+00:00

I am using EclipseLink MOXy and have a data structure that has child elements

  • 0

I am using EclipseLink MOXy and have a data structure that has child elements of the same data type. Now I don’t want to serialize the datastructure with infinite depth, but only the first level.

Here is some example code of the data structure:

package test;

import java.util.Collection;
import java.util.Vector;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement
public class MyClass {
    private int id;
    private String details;
    private Collection<MyClass> children = new Vector<MyClass>();

    public MyClass() {
    }

    public MyClass(int id, String details) {
        this.id = id;
        this.details = details;
    }

    @XmlElementWrapper
    @XmlElementRef
    public Collection<MyClass> getChildren() {
        return children;
    }

    public void addChild(MyClass child) {
        children.add(child);
    }

    public String getDetails() {
        return details;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setChildren(Collection<MyClass> children) {
        this.children = children;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public void setId(int id) {
        this.id = id;
    }
}

And my test program:

package test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Test {
    public static void main(String[] args) throws Exception {
        MyClass l1 = new MyClass(1, "Level 1");
        MyClass l2 = new MyClass(2, "Level 2");
        l1.addChild(l2);
        MyClass l3 = new MyClass(3, "Level 3");
        l2.addChild(l3);

        JAXBContext jc = JAXBContext.newInstance(MyClass.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(l1, System.out);
    }
}

The following XML is generated:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myClass id="1">
    <children>
        <myClass id="2">
            <children>
                <myClass id="3">
                    <children/>
                    <details>Level 3</details>
                </myClass>
            </children>
            <details>Level 2</details>
        </myClass>
    </children>
    <details>Level 1</details>
</myClass>

However, I’d like the xml too look like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myClass id="1">
    <children>
        <myClass id="2">
            <details>Level 2</details>
        </myClass>
    </children>
    <details>Level 1</details>
</myClass>

Thanks.

  • 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-09T01:09:33+00:00Added an answer on June 9, 2026 at 1:09 am

    To accomplish this use case we will leverage two concepts from JAXB: XmlAdapter and Marshaller.Listener.

    MyClassAdapter

    We will leverage the default JAXB behaviour of not marshalling an element for a null value. To do this we will implement an XmlAdapter that returns null after a specified level has been reached. To count the levels we will create a Marshaller.Listener.

    package forum11769758;
    
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class MyClassAdapter extends XmlAdapter<MyClass, MyClass>{
    
        private int levels;
        private MyMarshallerListener marshallerListener;
    
        public MyClassAdapter() {
        }
    
        public MyClassAdapter(int levels) {
            this.levels = levels;
        }
    
        public Marshaller.Listener getMarshallerListener() {
            if(null == marshallerListener) {
                marshallerListener = new MyMarshallerListener();
            }
            return marshallerListener;
        }
    
        @Override
        public MyClass marshal(MyClass myClass) throws Exception {
            if(null == marshallerListener || marshallerListener.getLevel() < levels) {
                return myClass;
            }
            return null;
        }
    
        @Override
        public MyClass unmarshal(MyClass myClass) throws Exception {
            return myClass;
        }
    
        static class MyMarshallerListener extends Marshaller.Listener {
    
            private int level = 0;
    
            public int getLevel() {
                return level;
            }
    
            @Override
            public void afterMarshal(Object object) {
                if(object instanceof MyClass) {
                    level--;
                }
            }
    
            @Override
            public void beforeMarshal(Object object) {
                if(object instanceof MyClass) {
                    level++;
                }
            }
    
        }
    
    }
    

    MyClass

    The @XmlJavaTypeAdapter annotation is used to specify that an XmlAdapter should be used.

    package forum11769758;
    
    import java.util.*;
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlAccessorType(XmlAccessType.PROPERTY)
    @XmlRootElement
    public class MyClass {
    
        private int id;
        private String details;
        private Collection<MyClass> children = new Vector<MyClass>();
    
        public MyClass() {
        }
    
        public MyClass(int id, String details) {
            this.id = id;
            this.details = details;
        }
    
        @XmlElementWrapper
        @XmlElementRef
        @XmlJavaTypeAdapter(MyClassAdapter.class)
        public Collection<MyClass> getChildren() {
            return children;
        }
    
        public void addChild(MyClass child) {
            children.add(child);
        }
    
        public String getDetails() {
            return details;
        }
    
        @XmlAttribute
        public int getId() {
            return id;
        }
    
        public void setChildren(Collection<MyClass> children) {
            this.children = children;
        }
    
        public void setDetails(String details) {
            this.details = details;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
    }
    

    Test

    Since we need to use the XmlAdapter in a stateful way, we will set an instance of it on the Marshaller, we will also set the instance of Marshaller.Listener we created on the Marshaller.

    package forum11769758;
    
    import javax.xml.bind.*;
    
    public class Test {
    
        public static void main(String[] args) throws Exception {
            MyClass l1 = new MyClass(1, "Level 1");
            MyClass l2 = new MyClass(2, "Level 2");
            l1.addChild(l2);
            MyClass l3 = new MyClass(3, "Level 3");
            l2.addChild(l3);
    
            JAXBContext jc = JAXBContext.newInstance(MyClass.class);
            Marshaller marshaller = jc.createMarshaller();
            MyClassAdapter myClassAdapter = new MyClassAdapter(2);
            marshaller.setAdapter(myClassAdapter);
            marshaller.setListener(myClassAdapter.getMarshallerListener());
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(l1, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <myClass id="1">
       <children>
          <myClass id="2">
             <children/>
             <details>Level 2</details>
          </myClass>
       </children>
       <details>Level 1</details>
    </myClass>
    

    For More Information

    The following articles expand on the topics discussed in this answer:

    • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html
    • http://blog.bdoughan.com/2011/09/mixing-nesting-and-references-with.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using eclipselink for JPA . I have an entity, which has a
Using Eclipselink/MOXy 2.3 i have following usecase in marshalling to XML: abstract class MyAbstract
I want to know how to implement having List of interface Type using EclipseLink
Has anyone got EclipseLink MOXy (I'm using eclipselink 2.1.0) to work with Java 5?
To solve another problem I have moved from using Jersey to EclipseLink MOXy to
I have following use-case for marshalling a POJO to XML using Eclipselink MOXy 2.3:
I'm currently using Eclipselink, but I know now days most JPA implementations have been
I am using EclipseLink dynamic moxy for jaxb. When I try to set an
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
Using a populated Table Type as the source for a TSQL-Merge. I want to

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.