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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:16:41+00:00 2026-05-22T17:16:41+00:00

I have a list setter on a class that’s annotated with both @XmlElementWrapper(name =

  • 0

I have a list setter on a class that’s annotated with both @XmlElementWrapper(name = “foos”) and @XmlElement(name = “foo”).

When I unmarshall XML that has no <foos></foos> or <foo/> elements, the setter is called and passed an empty list. Is there a way to get the following?:

  • When there is no <foos/>, do not call the setter. Or if the setter must be called, pass null.
  • When <foos/> is present but empty, pass an empty list to the setter.
  • When <foos> has one or more child <foo/> elements, pass a populated list.
  • 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-22T17:16:41+00:00Added an answer on May 22, 2026 at 5:16 pm

    You could use an XmlAdapter for this use case:

    input1.xml

    When there is no , do not call
    the setter. Or if the setter must be
    called, pass null.

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <child/>
    </root>
    

    input2.xml

    When is present but empty,
    pass an empty list to the setter.

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <child>
            <foos/>
        </child>
    </root>
    

    input3.xml

    When has one or more child
    elements, pass a populated
    list.

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
        <child>
            <foos>
                 <foo>Hello World</foo>
           </foos>
       </child>
    </root>
    

    Root

    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Root {
    
        private Child child;
    
        @XmlJavaTypeAdapter(ChildAdapter.class)
        public Child getChild() {
            return child;
        }
    
        public void setChild(Child child) {
            this.child = child;
        }
    
    }
    

    Child

    import java.util.List;
    
    public class Child {
    
        private List<String> strings;
    
        public List<String> getStrings() {
            return strings;
        }
    
        public void setStrings(List<String> strings) {
            System.out.println("setStrings");
            this.strings = strings;
        }
    
    }
    

    ChildAdapter

    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class ChildAdapter extends XmlAdapter<ChildAdapter.AdaptedChild, Child> {
    
        public static class AdaptedChild {
            public Foos foos;
        }
    
        public static class Foos {
            public List<String> foo;
        }
    
        @Override
        public Child unmarshal(AdaptedChild adaptedChild) throws Exception {
            Child child = new Child();
            Foos foos = adaptedChild.foos;
            if(null != foos) {
                List<String> foo = foos.foo;
                if(null == foo) {
                    child.setStrings(new ArrayList<String>());
                } else {
                    child.setStrings(foos.foo);
                }
            }
            return child;
        }
    
        @Override
        public AdaptedChild marshal(Child child) throws Exception {
            AdaptedChild adaptedChild = new AdaptedChild();
            List<String> strings = child.getStrings();
            if(null != strings) {
                Foos foos = new Foos();
                foos.foo = strings;
                adaptedChild.foos = foos;
            }
            return adaptedChild;
        }
    
    }
    

    Demo

    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            Object o;
    
            o = unmarshaller.unmarshal(new File("input1.xml"));
            marshaller.marshal(o, System.out);
    
            o = unmarshaller.unmarshal(new File("input2.xml"));
            marshaller.marshal(o, System.out);
    
            o = unmarshaller.unmarshal(new File("input3.xml"));
            marshaller.marshal(o, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <child/>
    </root>
    setStrings
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <child>
          <foos/>
       </child>
    </root>
    setStrings
    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <child>
          <foos>
             <foo>Hello World</foo>
          </foos>
       </child>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ObservableCollection<Foo> that is bound to an ItemsControl (basically displaying a list). Foo
in my Silverlight 4 application, I have a class myClass that contains a list
For classes that have a long list of setters that are used frequently, I
i have list of rows that user select and i want to delete them,
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
i have an sql query that has one unnamed column as a list of
What I have in my project is the binary file that contains list of
I have a list of rather meaningless codes that I'm processing with a VB.NET
I have a rather unique class that allows its child classes to declare virtual
I have a list with class objects say List a[10]; class Hello extends abc

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.