|
Replies:
4
-
Last Post:
Apr 11, 2008 12:40 PM
by: davisford
|
|
|
|
|
|
|
different behaviour between JAXB 2.0 and JAXB 2.1
Posted:
Feb 19, 2008 12:34 PM
|
|
|
Hi,
after switching to JDK 6u4 and JAXB 2.1 we noticed an incompatible behaviour with regard to JAXB 2 when unmarshalling list data from XML. I tried to extract a test case which works on JDK 6u1 with JAXB 2.0, but not on JDKu4 with JAXB 2.1.
Consider the following annotated class: --- import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient;
@XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "container") public class Container { @XmlTransient private List<String> stringList;
@XmlElement(name = "elem") public List<String> getList() { return stringList; }
public void setList(List<String> newList) { // ATTENTION: for some other reason we need to make a list copy here !!! final List<String> copyList = new ArrayList<String>(); copyList.addAll(newList); stringList = copyList; } } ---- ... with the following generated schema (jaxbtest.xsd): --- <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="container" type="container"/> <xs:complexType name="container"> <xs:sequence> <xs:element name="elem" type="xs:string" maxOccurs="unbounded" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema> --- ...and the following example data file (data.xml): --- <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <container> <elem>one</elem> <elem>two</elem> <elem>three</elem> <elem>four</elem> <elem>five</elem> </container> --- ...and the following test case: --- import java.io.InputStream;
import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory;
import junit.framework.TestCase;
public class JaxbTestCase extends TestCase { public void testLoad() throws Exception { final Container container = loadFromXML("/data.xml"); assertNotNull(container); assertNotNull(container.getList()); assertEquals(5, container.getList().size()); }
private Container loadFromXML(final String resource) throws Exception { final Schema sk = getSchema(); final JAXBContext context = JAXBContext.newInstance(Container.class); final Unmarshaller um = context.createUnmarshaller(); um.setSchema(sk); final InputStream istr = Container.class.getResourceAsStream(resource); final Container result = (Container) um.unmarshal(istr); istr.close(); return result; }
private Schema getSchema() throws Exception { final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); return schemaFactory.newSchema(getClass().getResource("/jaxbtest.xsd")); } } ---
The test fails with JAXB 2.1: the list does not contain any elements. The cause is the list copy in method Container.setList(...). JAXB 2.0 obviously can deal with it.
Which JAXB version behaves wrong? Do we anything wrong in user code?
Thanks, Holger
|
|
|
|
|
|
|
Re: different behaviour between JAXB 2.0 and JAXB 2.1
Posted:
Feb 21, 2008 8:06 AM
in response to: brands
|
|
|
Should I add an issue for this or what do the JAXB committers recommend?
Thanks, Holger
|
|
|
|
|
|
|
|
Re: different behaviour between JAXB 2.0 and JAXB 2.1
Posted:
Feb 21, 2008 5:29 PM
in response to: brands
|
|
|
May be you should file a bug for JAXB.
|
|
|
|
|
|
|
|
Re: different behaviour between JAXB 2.0 and JAXB 2.1
Posted:
Apr 11, 2008 12:40 PM
in response to: brands
|
|
|
hi, did you find a resolution for this problem, as I am experiencing the same thing
|
|
|
|
|