users@jaxb.java.net

RE: More unmarshalling errors

From: Spies, Brennan <Brennan.Spies_at_ejgallo.com>
Date: Mon, 29 Oct 2007 13:43:05 -0700

Lexi,

 

Thanks, dumb error on my part. When I add the setter methods, though, when I
deserialize an example XML file, like

 

<Job>

            <Tasks>

                        <UserTask> ... </UserTask>

                        <AutomatedTask> ... </AutomatedTask>

            </Tasks>

            <Transitions>

                        <Transition to="..."/>

                        <BranchedTransition> ... </BranchedTransition>

            </Transitions>

</Job>

 

I am getting empty lists (using JAXB-RI 2.1.5)! This is not a problem in
cases where I use the wrapper around @XmlElement...but can't do that here
since I am deserializing heterogeneous child elements.

 

Again, the code snippets are:

 

      @XmlElementWrapper(name="Tasks",
namespace="urn:ejgallo:workflow:create", required=true)

      @XmlAnyElement(lax=true)

      public Collection<NodeDefinition<? extends Node>> getNodeDefinitions()
{

            return nodeDefinitions;

      }

 

      @XmlElementWrapper(name="Transitions",
namespace="urn:ejgallo:workflow:create", required=true)

      @XmlAnyElement(lax=true)

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

            return transitions;

      }

 

The hierarchy looks like:

 

NodeDefinition (abstract) <-- UserTaskDefinition

                                                <-- AutomatedTaskDefinition

                                                etc.

 

TransitionDefinition (interface) <-- AbstractTransitionDefinition <--
SimpleTransitionDefinition

 
<-- BranchedTransitionDefinition

 

 

Thanks,

 

Brennan

 

 

 

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

 

Brennan Spies