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

  • Home
  • SEARCH
  • 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 7158741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:06:59+00:00 2026-05-28T13:06:59+00:00

The goal is to produce the following XML with JAXB <foo> <bar>string data</bar> <bar>binary

  • 0

The goal is to produce the following XML with JAXB

<foo>
   <bar>string data</bar>
   <bar>binary data</bar>
</foo>

Is there a workaround to allow generic @XmlValue fields (I need to store byte[] and String data)? Below is what I desire:

@XmlRootElement
public class Foo {
    private @XmlElement List<Bar> bars;
}

@XmlRootElement
public class Bar<T> {
    private @XmlValue T value;  // (*)
}

But I get this exception

(*) IllegalAnnotationException:
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.

  • 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-28T13:07:00+00:00Added an answer on May 28, 2026 at 1:07 pm

    You could leverage an XmlAdapter for this use case instead of @XmlValue:

    BarAdapter

    package forum8807296;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class BarAdapter extends XmlAdapter<Object, Bar<?>> {
    
        @Override
        public Bar<?> unmarshal(Object v) throws Exception {
            if(null == v) {
                return null;
            }
            Bar<Object> bar = new Bar<Object>();
            bar.setValue(v);
            return bar;
        }
    
        @Override
        public Object marshal(Bar<?> v) throws Exception {
            if(null == v) {
                return null;
            }
            return v.getValue();
        }
    
    }
    

    Foo

    The XmlAdapter is associated with the bars property using the @XmlJavaTypeAdapter annotation:

    package forum8807296;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Foo {
        private List<Bar> bars;
    
        @XmlElement(name="bar")
        @XmlJavaTypeAdapter(BarAdapter.class)
        public List<Bar> getBars() {
            return bars;
        }
    
        public void setBars(List<Bar> bars) {
            this.bars = bars;
        }
    
    }
    

    Bar

    package forum8807296;
    
    public class Bar<T> {
        private T value;
    
        public T getValue() {
            return value;
        }
    
        public void setValue(T value) {
            this.value = value;
        }
    }
    

    Demo

    You can test this example using the following demo code:

    package forum8807296;
    
    import java.util.ArrayList;
    import java.util.List;
    
    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(Foo.class);
    
            Foo foo = new Foo();
            List<Bar> bars = new ArrayList<Bar>();
            foo.setBars(bars);
    
            Bar<String> stringBar = new Bar<String>();
            stringBar.setValue("string data");
            bars.add(stringBar);
    
            Bar<byte[]> binaryBar = new Bar<byte[]>();
            binaryBar.setValue("binary data".getBytes());
            bars.add(binaryBar);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(foo, System.out);
        }
    
    }
    

    Output

    Note how the output includes the xsi:type attributes to preserve the type of the value. You can eliminate the the xsi:type attribute by having your XmlAdapter return String instead of Object, if you do this you will need handle the conversion from String to the appropriate type yourself for the unmarshal operation:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <foo>
        <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">string data</bars>
        <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:base64Binary">YmluYXJ5IGRhdGE=</bars>
    </foo>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML <AllVendors xmlns=http://important/data> <Vendor> <VendorId>1</VendorId> <VendorName>Vendor A</VendorName> </Vendor> <Vendor> <VendorId>2</VendorId>
So to give a little context, my goal here is to produce a binary
goal: I have the string 1234432144 I want to only replace the first 2
Is there a practical algorithm that gives multiplication chains To clarify, the goal is
I have to create object model for following XMLs: XML sample 1: <InvoiceAdd> <TxnDate>2009-01-21</TxnDate>
Goal: Show data from server in wxPython GUI on client Newcomer to Twisted. I
Given the following RegEx expression, testing this on regexlib.com with the string 2rocks produces
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
Goal: to programmatically determine the sizes (in bytes) of the fields of a class.

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.