xsd - JAXB binding to remove propOrder -
i have written xsd:
<?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetnamespace="http://api.synthesys/models/generated/simple/chat" xmlns:drsc="http://api.synthesys/models/generated/simple/chat" xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/simplify" jaxb:extensionbindingprefixes="simplify" jaxb:version="2.1"> <xsd:element name="conversation"> <xsd:complextype> <xsd:sequence> <xsd:element name="start-time" type="xsd:datetime" minoccurs="1" maxoccurs="1"/> <xsd:choice minoccurs="0" maxoccurs="unbounded"> <!-- code generation, allowing direct , distinct access messages , events --> <xsd:annotation> <xsd:appinfo> <simplify:as-element-property/> </xsd:appinfo> </xsd:annotation> <!--messages--> <xsd:element name="message"> <xsd:complextype> <xsd:sequence> <xsd:element name="author" minoccurs="1" maxoccurs="1" type="xsd:string"/> <xsd:element name="text" minoccurs="1" maxoccurs="1" type="xsd:string"/> </xsd:sequence> </xsd:complextype> </xsd:element> <!--room events: entering or exiting room--> <xsd:element name="event"> <xsd:complextype> <xsd:sequence> <xsd:element name="who" minoccurs="1" maxoccurs="1" type="xsd:string"/> <xsd:element name="what" minoccurs="1" maxoccurs="1" type="xsd:string"/> </xsd:sequence> </xsd:complextype> </xsd:element> </xsd:choice> <!--end time millis--> <xsd:element name="end-time" type="xsd:datetime" minoccurs="1" maxoccurs="1"/> </xsd:sequence> </xsd:complextype> </xsd:element> </xsd:schema>
and have written sample xml file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <mine:conversation xmlns:mine="http://api.synthesys/models/generated/simple/chat"> <start-time>2017-09-10t12:00:00.000z</start-time> <event> <who>john</who> <what>entered room</what> </event> <message> <author>john</author> <text>hello</text> </message> <event> <who>jane</who> <what>entered room</what> </event> <message> <author>jane</author> <text>goodbye, john</text> </message> <event> <who>jane</who> <what>left room</what> </event> <event> <who>john</who> <what>left room</what> </event> <end-time>2017-09-10t12:01:00.000z</end-time> </mine:conversation>
and maven-jaxb2-plugin
have been able generate class schema this:
@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "", proporder = { "starttime", "messages", "events", "endtime" }) @xmlrootelement(name = "conversation") public class conversation { @xmlelement(name = "start-time", required = true, type = string.class) @xmljavatypeadapter(adapter1 .class) @xmlschematype(name = "datetime") protected xmlgregoriancalendar starttime; @xmlelement(name = "message") protected list<conversation.message> messages; @xmlelement(name = "event") protected list<conversation.event> events; @xmlelement(name = "end-time", required = true, type = string.class) @xmljavatypeadapter(adapter1 .class) @xmlschematype(name = "datetime") protected xmlgregoriancalendar endtime;
the problem when try use jaxb unmarshall sample xml file this:
class marshalunmarshaltest extends specification { def "test"(){ setup: schemafactory sf = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri); jaxbcontext context = jaxbcontext.newinstance(synthesys.api.models.generated.simple.chat.conversation.class) marshaller marshaller = context.createmarshaller() unmarshaller unmarshaller = context.createunmarshaller() bytearrayoutputstream bos = new bytearrayoutputstream(); context.generateschema(new structureddatautils.structureddataschemaoutputresolver(bos)); schema schema = sf.newschema(new streamsource(new bytearrayinputstream(bos.tobytearray()))); marshaller.setschema(schema) unmarshaller.setschema(schema) file input = new file("src/test/resources/data/genericchat/simple-valid.xml") when: def object = unmarshaller.unmarshal(input.newinputstream()) then: object instanceof synthesys.api.models.generated.simple.chat.conversation } }
i error:
javax.xml.bind.unmarshalexception - linked exception: [org.xml.sax.saxparseexception; linenumber: 8; columnnumber: 14; cvc-complex-type.2.4.a: invalid content found starting element 'message'. 1 of '{event, end-time}' expected.] @ javax.xml.bind.helpers.abstractunmarshallerimpl.createunmarshalexception(abstractunmarshallerimpl.java:335) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.createunmarshalexception(unmarshallerimpl.java:563) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:249) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal(unmarshallerimpl.java:214) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:157) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:204) @ synthesys.api.models.generated.standard.chat.marshalunmarshaltest.test(marshalunmarshaltest.groovy:31) caused by: org.xml.sax.saxparseexception; linenumber: 8; columnnumber: 14; cvc-complex-type.2.4.a: invalid content found starting element 'message'. 1 of '{event, end-time}' expected. @ com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.createsaxparseexception(errorhandlerwrapper.java:203) @ com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.error(errorhandlerwrapper.java:134) @ com.sun.org.apache.xerces.internal.impl.xmlerrorreporter.reporterror(xmlerrorreporter.java:437) @ com.sun.org.apache.xerces.internal.impl.xmlerrorreporter.reporterror(xmlerrorreporter.java:368) @ com.sun.org.apache.xerces.internal.impl.xmlerrorreporter.reporterror(xmlerrorreporter.java:325) @ com.sun.org.apache.xerces.internal.impl.xs.xmlschemavalidator$xsierrorreporter.reporterror(xmlschemavalidator.java:458) @ com.sun.org.apache.xerces.internal.impl.xs.xmlschemavalidator.reportschemaerror(xmlschemavalidator.java:3237) @ com.sun.org.apache.xerces.internal.impl.xs.xmlschemavalidator.handlestartelement(xmlschemavalidator.java:1796) @ com.sun.org.apache.xerces.internal.impl.xs.xmlschemavalidator.startelement(xmlschemavalidator.java:746) @ com.sun.org.apache.xerces.internal.jaxp.validation.validatorhandlerimpl.startelement(validatorhandlerimpl.java:570) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.validatingunmarshaller.startelement(validatingunmarshaller.java:86) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.saxconnector.startelement(saxconnector.java:163) @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.startelement(abstractsaxparser.java:509) @ com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.scanstartelement(xmlnsdocumentscannerimpl.java:379) @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl$fragmentcontentdriver.next(xmldocumentfragmentscannerimpl.java:2786) @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl.next(xmldocumentscannerimpl.java:606) @ com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.next(xmlnsdocumentscannerimpl.java:117) @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scandocument(xmldocumentfragmentscannerimpl.java:510) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(xml11configuration.java:848) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(xml11configuration.java:777) @ com.sun.org.apache.xerces.internal.parsers.xmlparser.parse(xmlparser.java:141) @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.parse(abstractsaxparser.java:1213) @ com.sun.org.apache.xerces.internal.jaxp.saxparserimpl$jaxpsaxparser.parse(saxparserimpl.java:649) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:243) ... 4 more
what's frustrating me sample file validates when using xmllint --noout --schema path/to/schema.xsd path/to/sample.xml
. seems me main issue generated conversation
class defines proporder
more restrictive schema is.
can point me how can configure simplify plugin not generate proporder? or how can otherwise leverage bindings remove restriction? want maintain current behavior generated class has both list<message> messages
property , list<event> events
property, rather single list<object> messagesorevents
Comments
Post a Comment