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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:50:09+00:00 2026-05-26T09:50:09+00:00

I have to generate a xml element that can have as value any primitive

  • 0

I have to generate a xml element that can have as value any “primitive type” (xsd:string, xsd:boolean, etc). Examples:

<field xsi:type="xsd:string" name="aString">String Value</field>
<field xsi:type="xsd:date" name="aDate">2011-10-21</field>
...

So, I tried two implementations:

public class Field {
    @XmlAttribute
    private String name;

    @XmlValue
    Object value;
}

and …

public class Field<T> {
    @XmlAttribute
    private String name;

    @XmlValue
    T value;
}

I’m testing this with:

Marshaller marshaller = JAXBContext.newInstance(Field.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);

Field field = new Field();
field.name = "name";
field.value = "value";

ByteArrayOutputStream stream = new ByteArrayOutputStream();
marshaller.marshal(field, new PrintWriter(stream));
System.out.println(stream);

But I’m getting this NullPointerException when I try to instantiate the JAXBContext.

java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:165)
at com.sun.xml.bind.v2.runtime.property.ValueProperty.<init>(ValueProperty.java:77)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:106)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:179)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:515)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:330)
at 

The idea is allow schema validation for the field element (It is define in a schema must its type will be set in each instance). So, even is this a Bug (or not) … how JAXB will put the correct xsi:type to this field instance? I’m missing a concept here?

I know that maybe the problem is the usage of the @XmlValue because of this restrictions (from the javadoc):

  • At most one field or property can be annotated with the @XmlValue annotation.
  • @XmlValue can be used with the following annotations: XmlList. However this is redundant since XmlList maps a type to a simple schema type that derives by list just as XmlValue would.
  • If the type of the field or property is a collection type, then the collection item type must map to a simple schema type.
  • If the type of the field or property is not a collection type, then the type must map to a XML Schema simple type.

… because an Object or a generic T is not necessarily a XML Schema simple type, this approach seems not to be the correct one …

Thanks in advance …

  • 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-26T09:50:10+00:00Added an answer on May 26, 2026 at 9:50 am

    I have confirmed the issue you are seeing in both the reference and EclipseLink JAXB (MOXy) implementations of JAXB. The problem you are seeing is due to use of @XmlValue. If the value property was mapped as an @XmlElement you would see the xsi:type attribute appear as expected.

    I have entered the following bug to track this issue in EclipseLink JAXB (MOXy):

    • https://bugs.eclipse.org/361689

    Depending upon what your domain model looks like you may be interested in the @XmlPath extension from EclipseLink JAXB (MOXy):

    • http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html

    UPDATE

    This issue has now been fixed in the EclipseLink 2.3.3 and EclipseLink 2.4.0. The fix is available in these streams starting March 17, 2012 and can be obtained from:

    • http://www.eclipse.org/eclipselink/downloads/nightly.php
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML schema: <xsd:element name=Person> <xsd:complexType> <xsd:sequence> <xsd:element name=name type=xsd:string /> <xsd:element
How can I access this element: <input type=submit value=Save as XML onclick=some code goes
I have an XML document that I generate from an Entity Framework object. The
I have a Java string of XML content. I use Velocity to generate some
I have a schema fragment that looks like <xs:element name=dataValue> <xs:complexType> <xs:sequence> <xs:element name=value
I have an XSD that I created. A Transaction Header can one or more
I have around 6 individual xsd schemas that define one complex type each. I
I have an XML schema, where element Calling1 is defined like this: <xsd:element name=Calling1
I have to generate XML in the below format <objects> <items> <item =delete> <searchfields>
I have some php files in a server which generate xml code in them.

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.