Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » XML Schema Definition (XSD) » Problem unmarshalling to Java object(javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none))
Problem unmarshalling to Java object [message #1681795] Tue, 17 March 2015 13:11 Go to next message
Joseph Gagnon is currently offline Joseph GagnonFriend
Messages: 68
Registered: June 2013
Member
I apologize in advance for a lengthy posting. I figure the more detail I can provide, the more it might help. If this is the wrong forum to post this issue, kindly point me toward what would be more appropriate.

I am working with a small XML schema and use XJC to generate a Java API from the schema for use in an application. This schema is intended to represent configuration information for a messaging system's transport mechanism. The application reads and parses an XML file via JAXB and provides the resulting Java objects to the application logic. The application supports multiple transport types (e.g. ActiveMQ, RTI DDS, UDP) and the intent is for the config file to be able to contain elements for any of the supported transport types. Each transport has its own set of schema types to represent its configuration information.

I'm running into a problem when the program tries to unmarshal the XML config file. I know that the XML validates against the schema. I get the following exception:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://messaging/1.1", local:"TransportConfig"). Expected elements are (none)

What does it mean by "expected elements are (none)"?

If I edit the root element Java class (TransportConfig.java), I see something interesting and wonder if this may be related to the problem:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TransportConfigType", propOrder = {
    "transportConfigs"
})
public class TransportConfigType {
    @XmlElements({
        @XmlElement(name = "ActiveMQTransportConfigs", type = ActiveMQTransportConfigsType.class),
        @XmlElement(name = "RTIDDSTransportConfigs", type = RTIDDSTransportConfigsType.class),
        @XmlElement(name = "UDPTransportConfigs", type = UDPTransportConfigsType.class)
    })
    protected List<Object> transportConfigs;

    public List<Object> getTransportConfigs() {
        if (transportConfigs == null) {
            transportConfigs = new ArrayList<Object>();
        }
        return this.transportConfigs;
    }
}

Here is the schema file:

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msg="http://messaging/1.1" targetNamespace="http://messaging/1.1" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<!-- Elements -->
	<element name="TransportConfig" type="msg:TransportConfigType"/>
	<element name="ActiveMQBroker" type="msg:ActiveMQBrokerType"/>
	<element name="ActiveMQBrokers" type="msg:ActiveMQBrokersType"/>
	<element name="ActiveMQDestination" type="msg:ActiveMQDestinationType"/>
	<element name="ActiveMQDestinations" type="msg:ActiveMQDestinationsType"/>
	<element name="ActiveMQTransportConfig" type="msg:ActiveMQTransportConfigType"/>
	<element name="ActiveMQTransportConfigs" type="msg:ActiveMQTransportConfigsType"/>
	<element name="RTIDDSBroker" type="msg:RTIDDSBrokerType"/>
	<element name="RTIDDSBrokers" type="msg:RTIDDSBrokersType"/>
	<element name="RTIDDSDestination" type="msg:RTIDDSDestinationType"/>
	<element name="RTIDDSDestinations" type="msg:RTIDDSDestinationsType"/>
	<element name="RTIDDSTransportConfig" type="msg:RTIDDSTransportConfigType"/>
	<element name="RTIDDSTransportConfigs" type="msg:RTIDDSTransportConfigsType"/>
	<element name="UDPTransportConfig" type="msg:UDPTransportConfigType"/>
	<element name="UDPTransportConfigs" type="msg:UDPTransportConfigsType"/>
	<!-- Complex types -->
	<complexType name="TransportConfigType">
		<sequence>
		<choice minOccurs="1" maxOccurs="unbounded">
			<element ref="msg:ActiveMQTransportConfigs"/>
			<element ref="msg:RTIDDSTransportConfigs"/>
			<element ref="msg:UDPTransportConfigs"/>
		</choice>
		</sequence>
	</complexType>
	<complexType name="IDRefType">
		<attribute name="idref" type="xs:IDREF" use="required"/>
	</complexType>
	<complexType name="ActiveMQBrokerType">
		<!-- content omitted for brevity -->
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="ActiveMQBrokersType">
		<sequence>
			<element ref="msg:ActiveMQBroker" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
	<complexType name="ActiveMQDestinationType">
		<!-- content omitted for brevity -->
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="ActiveMQDestinationsType">
		<sequence>
			<element ref="msg:ActiveMQDestination" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
	<complexType name="ActiveMQTransportConfigType">
		<sequence>
			<element name="brokerID" type="msg:IDRefType" minOccurs="1" maxOccurs="1"/>
			<element name="destinationID" type="msg:IDRefType" minOccurs="1" maxOccurs="1"/>
			<element name="asyncRcvTimeout" type="long" minOccurs="1" maxOccurs="1"/>
		</sequence>
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="ActiveMQTransportConfigsType">
		<sequence>
			<element ref="msg:ActiveMQBrokers" minOccurs="1" maxOccurs="1"/>
			<element ref="msg:ActiveMQDestinations" minOccurs="1" maxOccurs="1"/>
			<element ref="msg:ActiveMQTransportConfig" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
	<complexType name="RTIDDSBrokerType">
		<!-- content omitted for brevity -->
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="RTIDDSBrokersType">
		<sequence>
			<element ref="msg:RTIDDSBroker" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
	<complexType name="RTIDDSDestinationType">
		<!-- content omitted for brevity -->
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="RTIDDSDestinationsType">
		<sequence>
			<element ref="msg:RTIDDSDestination" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
	<complexType name="RTIDDSTransportConfigType">
		<sequence>
			<element name="brokerID" type="msg:IDRefType"/>
			<element name="destinationID" type="msg:IDRefType"/>
			<element name="asyncRcvTimeout" type="long"/>
		</sequence>
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="RTIDDSTransportConfigsType">
		<sequence>
			<element ref="msg:RTIDDSBrokers"/>
			<element ref="msg:RTIDDSDestinations"/>
			<element ref="msg:RTIDDSTransportConfig" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
	<complexType name="UDPTransportConfigType">
		<!-- content omitted for brevity -->
		<attribute name="id" type="ID" use="required"/>
	</complexType>
	<complexType name="UDPTransportConfigsType">
		<sequence>
			<element ref="msg:UDPTransportConfig" minOccurs="1" maxOccurs="unbounded"/>
		</sequence>
	</complexType>
</schema>

and here is an example of the config file:

<?xml version="1.0" encoding="UTF-8"?>
<TransportConfig xmlns="http://messaging/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://messaging/1.1 transport-config.xsd">
	<ActiveMQTransportConfigs>
		<ActiveMQBrokers>
			<ActiveMQBroker id="broker1">
				<username>guest</username>
				<password>guest</password>
				<brokerURI>tcp://localhost:61616</brokerURI>
				<sessionPersisted>false</sessionPersisted>
				<acknowledgeMode>AUTO_ACKNOWLEDGE</acknowledgeMode>
			</ActiveMQBroker>
		</ActiveMQBrokers>
		<ActiveMQDestinations>
			<ActiveMQDestination id="default_dest">
				<name>DefaultTopic</name>
				<deliveryMode>NON_PERSISTENT</deliveryMode>
			</ActiveMQDestination>
		</ActiveMQDestinations>
		<ActiveMQTransportConfig id="default">
			<brokerID idref="broker1" />
			<destinationID idref="default_dest" />
			<asyncRcvTimeout>100</asyncRcvTimeout>
		</ActiveMQTransportConfig>
	</ActiveMQTransportConfigs>
</TransportConfig>

Re: Problem unmarshalling to Java object [message #1682075 is a reply to message #1681795] Tue, 17 March 2015 15:32 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29540
Registered: July 2009
Senior Member
Joseph,

Sorry, this forum is for help using Eclipse's XSD API. Perhaps
something like Stackoverflow would be better.


On 17/03/2015 2:11 PM, Joseph Gagnon wrote:
> I apologize in advance for a lengthy posting. I figure the more detail
> I can provide, the more it might help. If this is the wrong forum to
> post this issue, kindly point me toward what would be more appropriate.
>
> I am working with a small XML schema and use XJC to generate a Java
> API from the schema for use in an application. This schema is intended
> to represent configuration information for a messaging system's
> transport mechanism. The application reads and parses an XML file via
> JAXB and provides the resulting Java objects to the application logic.
> The application supports multiple transport types (e.g. ActiveMQ, RTI
> DDS, UDP) and the intent is for the config file to be able to contain
> elements for any of the supported transport types. Each transport has
> its own set of schema types to represent its configuration information.
>
> I'm running into a problem when the program tries to unmarshal the XML
> config file. I know that the XML validates against the schema. I get
> the following exception:
>
>
> javax.xml.bind.UnmarshalException: unexpected element
> (uri:"http://messaging/1.1", local:"TransportConfig"). Expected
> elements are (none)
>
> What does it mean by "expected elements are (none)"?
>
> If I edit the root element Java class (TransportConfig.java), I see
> something interesting and wonder if this may be related to the problem:
>
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "TransportConfigType", propOrder = {
> "transportConfigs"
> })
> public class TransportConfigType {
> @XmlElements({
> @XmlElement(name = "ActiveMQTransportConfigs", type =
> ActiveMQTransportConfigsType.class),
> @XmlElement(name = "RTIDDSTransportConfigs", type =
> RTIDDSTransportConfigsType.class),
> @XmlElement(name = "UDPTransportConfigs", type =
> UDPTransportConfigsType.class)
> })
> protected List<Object> transportConfigs;
>
> public List<Object> getTransportConfigs() {
> if (transportConfigs == null) {
> transportConfigs = new ArrayList<Object>();
> }
> return this.transportConfigs;
> }
> }
>
> Here is the schema file:
>
>
> <schema xmlns="http://www.w3.org/2001/XMLSchema"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:msg="http://messaging/1.1"
> targetNamespace="http://messaging/1.1" elementFormDefault="qualified"
> attributeFormDefault="unqualified">
> <!-- Elements -->
> <element name="TransportConfig" type="msg:TransportConfigType"/>
> <element name="ActiveMQBroker" type="msg:ActiveMQBrokerType"/>
> <element name="ActiveMQBrokers" type="msg:ActiveMQBrokersType"/>
> <element name="ActiveMQDestination"
> type="msg:ActiveMQDestinationType"/>
> <element name="ActiveMQDestinations"
> type="msg:ActiveMQDestinationsType"/>
> <element name="ActiveMQTransportConfig"
> type="msg:ActiveMQTransportConfigType"/>
> <element name="ActiveMQTransportConfigs"
> type="msg:ActiveMQTransportConfigsType"/>
> <element name="RTIDDSBroker" type="msg:RTIDDSBrokerType"/>
> <element name="RTIDDSBrokers" type="msg:RTIDDSBrokersType"/>
> <element name="RTIDDSDestination" type="msg:RTIDDSDestinationType"/>
> <element name="RTIDDSDestinations"
> type="msg:RTIDDSDestinationsType"/>
> <element name="RTIDDSTransportConfig"
> type="msg:RTIDDSTransportConfigType"/>
> <element name="RTIDDSTransportConfigs"
> type="msg:RTIDDSTransportConfigsType"/>
> <element name="UDPTransportConfig"
> type="msg:UDPTransportConfigType"/>
> <element name="UDPTransportConfigs"
> type="msg:UDPTransportConfigsType"/>
> <!-- Complex types -->
> <complexType name="TransportConfigType">
> <sequence>
> <choice minOccurs="1" maxOccurs="unbounded">
> <element ref="msg:ActiveMQTransportConfigs"/>
> <element ref="msg:RTIDDSTransportConfigs"/>
> <element ref="msg:UDPTransportConfigs"/>
> </choice>
> </sequence>
> </complexType>
> <complexType name="IDRefType">
> <attribute name="idref" type="xs:IDREF" use="required"/>
> </complexType>
> <complexType name="ActiveMQBrokerType">
> <!-- content omitted for brevity -->
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="ActiveMQBrokersType">
> <sequence>
> <element ref="msg:ActiveMQBroker" minOccurs="1"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> <complexType name="ActiveMQDestinationType">
> <!-- content omitted for brevity -->
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="ActiveMQDestinationsType">
> <sequence>
> <element ref="msg:ActiveMQDestination" minOccurs="1"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> <complexType name="ActiveMQTransportConfigType">
> <sequence>
> <element name="brokerID" type="msg:IDRefType"
> minOccurs="1" maxOccurs="1"/>
> <element name="destinationID" type="msg:IDRefType"
> minOccurs="1" maxOccurs="1"/>
> <element name="asyncRcvTimeout" type="long" minOccurs="1"
> maxOccurs="1"/>
> </sequence>
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="ActiveMQTransportConfigsType">
> <sequence>
> <element ref="msg:ActiveMQBrokers" minOccurs="1"
> maxOccurs="1"/>
> <element ref="msg:ActiveMQDestinations" minOccurs="1"
> maxOccurs="1"/>
> <element ref="msg:ActiveMQTransportConfig" minOccurs="1"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> <complexType name="RTIDDSBrokerType">
> <!-- content omitted for brevity -->
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="RTIDDSBrokersType">
> <sequence>
> <element ref="msg:RTIDDSBroker" minOccurs="1"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> <complexType name="RTIDDSDestinationType">
> <!-- content omitted for brevity -->
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="RTIDDSDestinationsType">
> <sequence>
> <element ref="msg:RTIDDSDestination" minOccurs="1"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> <complexType name="RTIDDSTransportConfigType">
> <sequence>
> <element name="brokerID" type="msg:IDRefType"/>
> <element name="destinationID" type="msg:IDRefType"/>
> <element name="asyncRcvTimeout" type="long"/>
> </sequence>
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="RTIDDSTransportConfigsType">
> <sequence>
> <element ref="msg:RTIDDSBrokers"/>
> <element ref="msg:RTIDDSDestinations"/>
> <element ref="msg:RTIDDSTransportConfig"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> <complexType name="UDPTransportConfigType">
> <!-- content omitted for brevity -->
> <attribute name="id" type="ID" use="required"/>
> </complexType>
> <complexType name="UDPTransportConfigsType">
> <sequence>
> <element ref="msg:UDPTransportConfig" minOccurs="1"
> maxOccurs="unbounded"/>
> </sequence>
> </complexType>
> </schema>
>
> and here is an example of the config file:
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <TransportConfig xmlns="http://messaging/1.1"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://messaging/1.1 transport-config.xsd">
> <ActiveMQTransportConfigs>
> <ActiveMQBrokers>
> <ActiveMQBroker id="broker1">
> <username>guest</username>
> <password>guest</password>
> <brokerURI>tcp://localhost:61616</brokerURI>
> <sessionPersisted>false</sessionPersisted>
> <acknowledgeMode>AUTO_ACKNOWLEDGE</acknowledgeMode>
> </ActiveMQBroker>
> </ActiveMQBrokers>
> <ActiveMQDestinations>
> <ActiveMQDestination id="default_dest">
> <name>DefaultTopic</name>
> <deliveryMode>NON_PERSISTENT</deliveryMode>
> </ActiveMQDestination>
> </ActiveMQDestinations>
> <ActiveMQTransportConfig id="default">
> <brokerID idref="broker1" />
> <destinationID idref="default_dest" />
> <asyncRcvTimeout>100</asyncRcvTimeout>
> </ActiveMQTransportConfig>
> </ActiveMQTransportConfigs>
> </TransportConfig>
>
>
Previous Topic:Xsd to Ecore Mapping: attributes with simple types
Next Topic:Generate XML instance from XML schema [programmatically]
Goto Forum:
  


Current Time: Fri Sep 21 04:42:05 GMT 2018

Powered by FUDForum. Page generated in 0.02302 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top