Community
Participate
Working Groups
Setup a (relatively complex) schema that imports an other schema: when the instance doc is unmarshalled, the DynamicEntity fields are wrapped in JAXBElement;s: static final String SRVC_SCHEMA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xsd:schema " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "targetNamespace=\"urn:simpleService\" " + "xmlns=\"urn:simpleService\" " + "xmlns:srvc=\"urn:simpleService\" " + "xmlns:ns1=\"urn:simpletable\" " + "elementFormDefault=\"qualified\">" + "<xsd:import namespace=\"urn:simpletable\" schemaLocation=\"simple.xsd\"/>" + "<xsd:complexType name=\"findByPrimaryKey_simpletableTypeResponseType\">" + "<xsd:sequence>" + "<xsd:element name=\"result\">" + "<xsd:complexType>" + "<xsd:sequence>" + "<xsd:element minOccurs=\"0\" ref=\"ns1:simpletableType\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "</xsd:element>" + "</xsd:sequence>" + "</xsd:complexType>" + "<xsd:complexType name=\"findByPrimaryKey_simpletableTypeRequestType\">" + "<xsd:sequence>" + "<xsd:element name=\"id\" type=\"xsd:decimal\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "<xsd:element name=\"findByPrimaryKey_simpletableTypeResponse\" type=\"srvc:findByPrimaryKey_simpletableTypeResponseType\"/>" + "<xsd:element name=\"findByPrimaryKey_simpletableType\" type=\"srvc:findByPrimaryKey_simpletableTypeRequestType\"/>" + "</xsd:schema>"; static final String SIMPLE_SCHEMA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xsd:schema " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "targetNamespace=\"urn:simpletable\" " + "xmlns=\"urn:simpletable\" " + "elementFormDefault=\"qualified\">" + "<xsd:complexType name=\"simpletableType\">" + "<xsd:sequence>" + "<xsd:element name=\"id\" type=\"xsd:decimal\"/>" + "<xsd:element name=\"name\" type=\"xsd:string\" minOccurs=\"0\" nillable=\"true\"/>" + "<xsd:element name=\"since\" type=\"xsd:date\" minOccurs=\"0\" nillable=\"true\"/>" + "</xsd:sequence>" + "</xsd:complexType>" + "<xsd:element name=\"simpletableType\" type=\"simpletableType\"/>" + "</xsd:schema>"; @Test public void testDynamicJAXB() throws FileNotFoundException, JAXBException { DynamicJAXBContext dynamicJAXBContext = DynamicJAXBContextFactory.createContextFromXSD(new StreamSource( new StringReader(SRVC_SCHEMA)), new MyEntityResolver2(), null, null); JAXBUnmarshaller unmarshaller = dynamicJAXBContext.createUnmarshaller(); Object o = unmarshaller.unmarshal(new StringReader(RESPONSE)); JAXBElement<DynamicEntity> responseEl = (JAXBElement<DynamicEntity>)o; DynamicEntity response = responseEl.getValue(); DynamicEntity result = response.get("result"); DynamicEntity simpleEntity = result.get("simpletableType"); System.out.println(); System.out.println(simpleEntity.get("id")); System.out.println(simpleEntity.get("name")); System.out.println(simpleEntity.get("since")); } static final String RESPONSE = "<ns1:findByPrimaryKey_simpletableTypeResponse xmlns:ns1=\"urn:simpleService\" xmlns=\"urn:simpletable\">" + "<ns1:result>" + "<simpletableType>" + "<id>1</id>" + "<name>mike</name>" + "<since>2001-12-25</since>" + "</simpletableType>" + "</ns1:result>" + "</ns1:findByPrimaryKey_simpletableTypeResponse>";
private class MyEntityResolver2 implements EntityResolver { //assume inline schemas are in same directory as wsdl public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { InputSource inputSource = new InputSource(new StringReader(SIMPLE_SCHEMA)); inputSource.setSystemId("file:/simple.xsd"); return inputSource; } }
It turns out that the behaviour described above is expected because of the nillable="true" attribute for "name" and "since". The JAXBElement tracks the 'isSet' status of the field in addition to regular values and NULL. A client could customize this behaviour with a JAXB binding file - so this bug is taking a bit of a left turn and will instead track adding the binding file to the API.
*** This bug has been marked as a duplicate of bug 331210 ***
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink