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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:47:06+00:00 2026-05-26T10:47:06+00:00

This is my scenario. I have a generic class: public class Tuple<T> extends ArrayList<T>

  • 0

This is my scenario. I have a generic class:

public class Tuple<T> extends ArrayList<T> {
  //...
  public Tuple(T ...members) {
    this(Arrays.asList(members));
  }

  @XmlElementWrapper(name = "tuple")
  @XmlElement(name = "value")
  public List<T> getList() {
    return this;
  }
}

And a child class:

public class StringTuple extends Tuple<String> {
  public StringTuple(String ...members) {
    super(members);
  }

  //explanation of why overriding this method soon ...
  @XmlElementWrapper(name = "tuple")
  @XmlElement(name = "value")
  @Override
  public List<String> getList() {
    return this;
  }
}

These classes are referenced here:

@XmlRootElement(namespace = "iv4e.xml.jaxb.model")
public class Relation {
  private Tuple<StringTuple> relationVars;
  //...
  @XmlElementWrapper(name = "allRelationVars")
  @XmlElement(name = "relationVarsList")
  public Tuple<StringTuple> getRelationVars() {
    return relationVars;
  }
}

Then a Relation object is created with something like:

Relation rel = new Relation();
rel.setRelationVars(new Tuple<StringTuple>(
  new StringTuple("RelationshipVar1"), new StringTuple("RelationshipVar2")));

After marshalling this object, the Xml output is the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:relation xmlns:ns2="iv4e.xml.jaxb.model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">

  <allRelationVars>
    <relationVarsList>
        <tuple>
            <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">RelationshipVar1</value>
        </tuple>
        <tuple>
            <value>RelationshipVar1</value>
        </tuple>
    </relationVarsList>
    <relationVarsList>
        <tuple>
            <value xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">RelationshipVar2</value>
        </tuple>
        <tuple>
            <value>RelationshipVar2</value>
        </tuple>
    </relationVarsList>
  </allRelationVars>

</ns2:relation>

So the value elements are duplicated!.

Now, the reason the class StringTuple overrides List<T> getList() with List<String> getList() is avoiding the annoying generated xmlns:xs attributes in every member of the list (the value elements in the xml document).
But then every member of the list is shown twice in the output. Apparently, it is because both the overridden parent method and the child method are annotated with @XmlElement.
So my main question is: there is a way to ignore overridden methods annotated with @XmlElement in Jaxb ? (considering that the overridding method is also annotated with @XmlElement)

I found an old post reporting quite a similar problem: http://old.nabble.com/@XmlElement-on-overridden-methods-td19101616.html , but I have not found any solution yet.
Also note that adding a @XmlTransient annotation to the getList method at the parent class (Tuple<T>) could solve this problem but will generate others, since the parent class is not abstract and is used alone in other contexts.

One side secondary question: is it possible to declare the xmlns:xs attribute at the root node instead of it -annoyingly- appearing in every node where it is needed? I know this can be done with the NamespacePrefixMapper class, but since it is a non standard, SUN internal class, I rather prefer to use a more implementation independent approach.

Thanks in advance for any feedback !

  • 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-26T10:47:07+00:00Added an answer on May 26, 2026 at 10:47 am

    You could use the following approach of marking the property @XmlTransient on the parent and @XmlElement on the child:

    Parent

    package forum7851052;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlRootElement
    public class Parent<T> {
    
        private List<T> item = new ArrayList<T>();
    
        @XmlTransient
        public List<T> getItem() {
            return item;
        }
    
        public void setItem(List<T> item) {
            this.item = item;
        }
    
    }
    

    IntegerChild

    package forum7851052;
    
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class IntegerChild extends Parent<Integer> {
    
        @Override
        @XmlElement
        public List<Integer> getItem() {
            return super.getItem();
        }
    
        @Override
        public void setItem(List<Integer> item) {
            super.setItem(item);
        }
    
    }
    

    StringChild

    package forum7851052;
    
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class StringChild extends Parent<String> {
    
        @Override
        @XmlElement
        public List<String> getItem() {
            return super.getItem();
        }
    
        @Override
        public void setItem(List<String> item) {
            super.setItem(item);
        }
    
    }
    

    Demo

    package forum7851052;
    
    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(Parent.class, IntegerChild.class, StringChild.class);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    
            IntegerChild integerChild = new IntegerChild();
            integerChild.getItem().add(1);
            integerChild.getItem().add(2);
            marshaller.marshal(integerChild, System.out);
    
            StringChild stringChild = new StringChild();
            stringChild.getItem().add("A");
            stringChild.getItem().add("B");
            marshaller.marshal(stringChild, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <integerChild>
        <item>1</item>
        <item>2</item>
    </integerChild>
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <stringChild>
        <item>A</item>
        <item>B</item>
    </stringChild>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 scenarios. This fails: class F<X> { public X X { get;
I have three classes: Generic, CFG, and Evaluator. Here's Generic: class Generic: public virtual
Imagine this scenario: You have a desktop and a laptop. The desktop has a
This is a question for a WSS/SharePoint guru. Consider this scenario: I have an
I have this scenario where I need data integrity in the physical database. For
we have this scenario: A server which contains needed data and client component which
Consider this scenario. I have my own website, that I use as my identifier,
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
The architecture for this scenario is as follows: I have a table of items
I was wondering how other people implemented this scenario. I have an internal rails

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.