users@jaxb.java.net

Re: More unmarshalling errors

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 29 Oct 2007 17:18:09 +0100

Hi.

> 2) Unmarshalling a simple element that has an xsi type (always getting
> null) to a generic type
>
> public class AttributeDefinition<T> {
>
> T value;
>
> @XmlElement(name="Value")
>
> public T getValue() {
>
> return value;
>
> }
>
> }
>
>
>
> Unmarshalling from this XML
>
>
>
> <Attribute name="num">
>
> <Value xsi:type="xs:int">5</Value>
>
> </Attribute>
>
>
>
> always gives me a null for Value.
>
>
>
> Suggestions?


You have forgotten the setter.

Consider the following working code:
=======================================
package org.jvnet.hyperjaxb3.sb.tests;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Attribute")
public class AttributeDefinition<T> {

        T value;

        @XmlElement(name = "Value")
        public T getValue() {
                return value;
        }
        
        public void setValue(T value) {
                this.value = value;
        }
}
=======================================
package org.jvnet.hyperjaxb3.sb.tests;

import javax.xml.bind.JAXBContext;

import junit.framework.Assert;
import junit.framework.TestCase;

public class AtributeDefinitionTest extends TestCase {

        @SuppressWarnings("unchecked")
        public void testUnmarshall() throws Exception {
                final JAXBContext context = JAXBContext
                                .newInstance(AttributeDefinition.class);

                AttributeDefinition<Integer> attributeDefinition =
(AttributeDefinition<Integer>) context
                                .createUnmarshaller().unmarshal(
                                                getClass().getResourceAsStream("attribute.xml"));

                Assert.assertEquals(Integer.valueOf(5), attributeDefinition.getValue());

        }
}
=======================================
<Attribute name="num">
        <Value xsi:type="xsd:int"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">5</Value>
</Attribute>


I believe you have a similar problem in the first case. Try something like:

public Collection<TransitionDefinition<? extends Node>>
getTransitions() {


                  if (transitions == null) transitions = new LinkedList<...>();
                  return transitions;

      }

Bye.
/lexi