Skip to main content

Problem with unmarshalling when annotation in super class ?

10 replies [Last post]
amarkel
Offline
Joined: 2004-05-13

I defined the following class:

@XmlAccessorType(AccessType.PROPERTY)
@XmlType( name = "node" )
public class Node {
private String imName;
private String daqName;

public Node( String theIMName, String theDAQName ){
imName = theIMName;
daqName = theDAQName;
}

@XmlAttribute( name = "InfoModel", required = true )
public String getIMName() { return imName; }

public void setIMName( String theName ) {
imName = theName;
}

@XmlAttribute( name = "DAQ" )
public String getDAQName() { return daqName; }

public void setDAQName( String theName ) {
daqName = theName;
}
}

And a subclass:

@XmlType( name = "argument" )
public class Arg extends Node {
public Arg( String theIMName, String theDAQName ) {
super( theIMName, theDAQName );
}
}

I wrote a test program that marshalls, unmarshalls an
instance into a document.

The following is the message on unmarshalling the
generated document:

file:/ws/work/users/arieh/x2-05/usr/proto/daqimmap.xsd
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: cvc-complex-type.2.1: Element 'argument' must have no character or element information item [children], because the type's content type is empty.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
.
.

Here is the generated xml code:
file:/ws/work/users/arieh/x2-05/scrk/usr/proto/com/sun/scn/asset/maps/impl/daqimmap.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

here is the xsd:

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

From the SAXParseException it is not clear what I am doing incorrectly.

Ideas ?

Thanks,

Arieh

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
amarkel
Offline
Joined: 2004-05-13

Here is the correct xsd (the type of 'argument'
is 'argument' - not 'method')

















sekhar
Offline
Joined: 2003-06-25

Some code/annotations still seem to be missing. You have a Map element and Map schema type in your generated schema but no corresponding Map type in your code. Post these if missing.

amarkel
Offline
Joined: 2004-05-13

Map file:

public class Map{
protected Collection argument;
protected String node;

public Map() {}

public Map( String theNode, Collection args ) {
node = theNode;
argument = args;
}

@XmlAttribute( name = "assetType", required = true )
public String getAssetType() { return node; }

public void setAssetType( String theType ) {
node = theType;
}

@XmlElement( type = Arg.class, name = "argument" )
public Collection getArguments() {
return arguments;
}

public void setArguments( Collection args ) {
arguments = args;
}
}

amarkel
Offline
Joined: 2004-05-13

Amended Map file:

public class Map{
protected Collection argument;
protected String node;

public Map() {}

public Map( String theNode, Collection args ) {
node = theNode;
argument = args;
}

@XmlAttribute( name = "node", required = true )
public String getAssetType() { return node; }

public void setAssetType( String theType ) {
node = theType;
}

@XmlElement( type = Arg.class, name = "argument" )
public Collection getArguments() {
return arguments;
}

public void setArguments( Collection args ) {
arguments = args;
}
}

sekhar
Offline
Joined: 2003-06-25

Thanks. That code corresponds to the generated schema much better ; the Map class needs to be annotated with @XmlRootElement and @XmlType(name="Map").

I don't quite see the same behavior as you do with my version of the RI (I did an unmarshal followed by a marshal). Nonetheless, there appears to be a bug in the RI - unmarshalling of Collection type does not appear to working even though the schema is generated for Collection (the contents of the Collection type are silently ignored).

I will file an issue in Issue Tracker to track this.

amarkel
Offline
Joined: 2004-05-13

I don't know how to proceed, as evidently, the behavior I am
seeing is different than the one you do.

A bit more investigation indicates that the cause of the
problem appears to be in the fact that the annotations are
done on the (refactored) 'node' class.

If I remove the 'extends node' and implement the methods
in it in each one of the classes, things work.

Arieh

amarkel
Offline
Joined: 2004-05-13

I appear to have found a kludged solution.

@XmlAccessorType( AccessType.PROPERTY )
@XmlType( name = "node" )
public class Node {
public String imName, daqName;

public Node() {}
public Node( String im, String daq ) {
imName = im; daqName = daq;
}

@XmlAttribute( name = "IM" )
public String getIM() { return imName; }
public void setIM( String im ) {
imName = im;
}

@XmlAttribute( name = "DAQ" )
public String getDAQ() { return imName; }
public void setDAQ( String daq ) {
daqName = daq;
}
}

@XmlAccessorType( AccessType.PROPERTY )
@XmlType( "argument" )
public class Arg extends Node {
public Node() {}
public Node( String im, String daq ) {
imName = im; daqName = daq;
}

public String getIM() { return super.getIM(); }
public void setIM( String im ) {
super.setIM( im );
}

public String getDAQ() { return super.getDAQ(); }
public void setDAQ( String daq ) {
super.setDAQ( daq );
}
}

@XmlAccessorType(AccessType.PROPERTY)
@XmlRootElement( name = "map" )
public class Map {
Collection arguments;

public Map() {}
public Map( Collection args ) {
arguments = arg;
}

@XmlElement( name = "argument" )
public Collection getArguments() {
return arguments;
}
public void setArguments( Collection args ) {
arguments = args;
}
}

The unmarshaller appears to expect that the subclass has the methods on it.

I would like to see this working without to explicitly declare the accessors/mutators in the subclass.

The schema generated, duplicates the elements declared in
the superclass:




















sekhar
Offline
Joined: 2003-06-25

I logged an issue to track both of the problems noted above.
https://jaxb.dev.java.net/issues/show_bug.cgi?id=92

sekhar
Offline
Joined: 2003-06-25

Your annotated classes cannot be mapped. A class is required to either have a zero arg constructor or a static zero arg factory method (specified using @XmlType annotation) or inthe absence of either of these adapted using @XmlJavaTypeAdapter. See the javadoc for @XmlType

https://jaxb.dev.java.net/nonav/jaxb20-pr/api/index.html

Also the generated schema does not match the code fragment (contains elements/types that have no counterparts in the code fragment ).

So either the version of the JAXB 2.0 RI you are using is not enforcing the above constraints or some of the annotations/code are missing from the example.

amarkel
Offline
Joined: 2004-05-13

I do have the no args constructor (just removed it when
I did the cut and paste).

You are right that my reply is incorrect, but it is not
what is causing the problem.

(Please assume that the no-args constructor is there).

Arieh