Skip to main content

@XmlAttribute/@XmlValue need to reference a Java type that maps to text in

9 replies [Last post]
thierry_giguere
Offline
Joined: 2007-05-25

Hello,

I have generated my Java classes that match the schema. Next step I try to marshal a basic sample. I caught the following exception right on the first line :

JAXBContext jc = JAXBContext.newInstance("myschema");

Exception in thread "main" junit.framework.AssertionFailedError: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExcep
tions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:

at protected java.lang.Object myschema.AttrOAnySimple.value

So what does it really mean and what step should I take to solve this issue. Is there any tutorial I should refer to? Why it's not working right out of the box?

Thanks!

------------

So far the only workaround I have found is customization. I have added the following schema and now it works. I got a mapping to String and an Adapter that convert String to/from String. Is this the best way to solve this issue?

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

Message was edited by: thierry_giguere

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

I guess this could be said as a "bug" in the spec. We should have bound that to String, not Object.

An issue in http://jaxb.dev.java.net/issues/ would be appreciated.

mshaffer55
Offline
Joined: 2007-02-07

Why do you say that? The original post does not provide the schema, but it sounds as though the attribute in question is untyped. An untyped attribute is by default mapped to xs:anySimpleType. I'm not sure it's reasonable to automatically map such an attribute to String. If it really is a String then it should be defined as type="xs:string" in the schema.

mshaffer55
Offline
Joined: 2007-02-07

My mistake. The JAXB spec says that, in the case of attributes, xs:anySimpleType should be mapped to java.lang.String, vs. to java.lang.Object for elements of xs:anySimpleType.

kohsuke
Offline
Joined: 2003-06-09

Or really? In that case this is a bug in the RI.

mshaffer55
Offline
Joined: 2007-02-07

Well that's the way I am interpreting it. See Table 6-1 in section 6.2.2 (JAXB 2.1).

kohsuke
Offline
Joined: 2003-06-09

... and when I tried the following schema:
[code]
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.0">







[/code]
I get the following output:
[code]
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {

@XmlAttribute
@XmlSchemaType(name = "anySimpleType")
protected String foo;

...
}
[/code]
So it looks like the RI is working as expected, at least as of 2.1.3.

So my question to the original poster is, which version of the JAXB RI are you using? Can you try the latest?

thierry_giguere
Offline
Joined: 2007-05-25

Release number :
ant -verbose jaxb

output the following at standard output stream :
[xjc] build id of XJC is 2.1.3-b01-fcs

The JDK :
Detected Java version: 1.5 in: c:\Program Files\Java\jdk1.5.0_11\jre

So I have try your basic schema with this setup and I get exactly the same result as you : a mapping to String.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {

@XmlAttribute
@XmlSchemaType(name = "anySimpleType")
protected String foo;

Now using the same shell session I try again my schema but run into issue http://forums.java.net/jive/thread.jspa?threadID=26841&tstart=15. So I can't test using the same setup and I need to use the old jaxb from jwsdp2.0

[xjc] build id of XJC is 2.0-b26-ea3

But just to compare I use the jwsdp2.0 jaxb release with your basic schema and got a mapping to String also. So it seems linked to schema more than jaxb release.

@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {

@XmlAttribute
protected String foo;

We notice some change in annotation so the setup for test seems to be ok.

kohsuke
Offline
Joined: 2003-06-09

Please don't use JAXB in JWSDP 2.0. It's way too old, and it's "early access" release.

mshaffer55
Offline
Joined: 2007-02-07

Do you have control over the schema? Do you really want to allow the attribute in question to be any simple type, or is it really always a string?