test, just ignore ________________________________
From: [email protected] [mailto:[email protected]] Sent: Sun 10/23/2011 10:06 PM To: [email protected] Subject: [castor-user] Clash of 'any' element with existing element descriptor Beantwoorden Allen beantwoorden Doorsturen Sluiten Help Van: [email protected] [[email protected]] Verzonden: do 20-10-2011 20:40 Aan: [email protected] CC: Onderwerp: [castor-user] any element clash with existing element while unmarshalling Bijlagen: Weergeven als webpagina Hi, I have a strange problem that seems to be caused by a clash of Element types. Scenario: - Castor binding classes generated from StandardBusinessDocument.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="http://pcs.portinfolink.nl/sbd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://pcs.portinfolink.nl/sbd" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="StandardBusinessDocument"> <xs:annotation> <xs:documentation>interchange wrapper</xs:documentation> </xs:annotation> <xs:complexType> <xs:sequence> <xs:element name="documentContent" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="contentBody"> <xs:complexType mixed="true"> <xs:sequence> <xs:any minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="totalNumberOfContentBodies" type="xs:integer" use="optional"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> - Also binding classes generated from another xsd (xliftmin.xsd) <?xml version="1.0" encoding="UTF-8"?> <xsd:schema targetNamespace="http://pcs.portinfolink.nl/xliftmin" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:element name="XLIFTMIN"> <xsd:complexType> <xsd:sequence> <xsd:element name="transportOrder"> <xsd:complexType> <xsd:sequence> <xsd:element name="pickUpAddressText" type="xsd:string" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="referenceAtPlaceOfDelivery" type="xsd:string" use="optional"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> - Send in a document to unmarshall. The document contains the element 'transportOrder' in the 'any' element. <?xml version="1.0" encoding="UTF-8"?> <!--Sample XML file generated by XMLSPY v5 rel. 4 U (http://www.xmlspy.com)-- <http://www.xmlspy.com)--/> > <StandardBusinessDocument> <documentContent totalNumberOfContentBodies="0"> <contentBody> <transportOrder> <test>Werner</test> </transportOrder> </contentBody> </documentContent> </StandardBusinessDocument> - Use test class that preloads descriptors Test.java: package nl.test; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.Properties; import nl.portinfolink.platform.xml.bindings.StandardBusinessDocument; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ResolverException; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.ValidationException; import org.exolab.castor.xml.XMLContext; public class Test { private static final String CASTORBUILDER_PROPERTIES_FILE = "castorbuilder.properties"; private static final String PROPERTY_NS_PACKAGES = "org.exolab.castor.builder.nspackages"; // The XMLContext used to get marshaller instances from private static XMLContext xmlContext = new XMLContext(); static { // preload descriptors, based on the castorbuilder.properties file Properties properties = new Properties(); try { InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(CASTORBUILDER_PROPERTIES_FILE); if (inputStream != null) // if in == null, use setXmlContext instead { properties.load(inputStream); String[] packages = properties.getProperty(PROPERTY_NS_PACKAGES).split(","); for (String keyValue : packages) { String packageName = keyValue.split("=")[1]; System.out.println("Preloading class descriptors for " + packageName); addPackage(packageName); } } } catch (IOException e) { System.out.println("Preloading packages failed, unable to load castorbuilder.properties" + e.getMessage()); } } /** * Add the supplied package to the xmlContext * * @param packageName */ public static void addPackage(String packageName) { try { xmlContext.addPackage(packageName); } catch (ResolverException e) { System.out.println("An exception occurred while adding packages to the XML Context" + e.getMessage()); } } public static void main(String[] args) { try { System.out.println("unmarshalling root instance:"); System.out.println(); for (int i = 0; i < 3; i++) { unmarshallDoc(i); } } catch (Exception e) { System.out.println(e); return; // e.printStackTrace(); } } private static void unmarshallDoc(int i) throws FileNotFoundException, MarshalException, ValidationException, IOException { Reader reader = new FileReader("xml/SBD.xml"); Unmarshaller unmarshaller = xmlContext.createUnmarshaller(); unmarshaller.setClass(StandardBusinessDocument.class); StandardBusinessDocument root = (StandardBusinessDocument) unmarshaller.unmarshal(reader); reader.close(); System.out.println(i); System.out.println(root.getDocumentContent(0).getContentBody().getAnyObject(0).toString()); } } - Output is Preloading class descriptors for nl.portinfolink.platform.xml.bindings.XL_Iftmin unmarshalling root instance: 0 nl.portinfolink.platform.xml.bindings.XL_Iftmin.TransportOrder@19ee1ac 1 <?xml version="1.0" encoding="UTF-8"?> <transportOrder><test>piet</test></transportOrder> 2 <?xml version="1.0" encoding="UTF-8"?> <transportOrder><test>piet</test></transportOrder> Conclusion: The first time the SBD.xml is unmarshalled it somehow refers to an nl.portinfolink.platform.xml.bindings.XL_Iftmin.TransportOrder object. Subsequent times the 'any' body which contains an element with the same name is unmarshalled 'correctly'. Any thoughts on what's happening and how it can be avoided? Enclosed an Eclipse project with the testcase Greetings, Huub

