I'm using the Java APIs and have generated code from a supplied IDL. The XSD defines the message type as a sequence of elements of type "x" having maxOccurs="3" . How do I parse the indeterminate number of elements in my on_data_available(DataReader reader) method?
IDL / XSD
<xs:complexType name="My.XType">
<xs:sequence>
<xs:element name="Y" type="My.YType" maxOccurs="3">
<xs:annotation>
<xs:documentation>maximum of 3 occurencees</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="X" type="My.XType">
<xs:annotation>
<xs:documentation>XLocation information</xs:documentation>
</xs:annotation>
</xs:element>
Code:
// ************************************************************************
// Implementation of the methods described in DataReaderListener interface
// ************************************************************************
public void on_data_available(DataReader reader) {
XTypeDataReader xReader = XTypeDataReader reader;
try
{
xReader.take(
_dataSeq,
_infoSeq,
ResourceLimitsQosPolicy.LENGTH_UNLIMITED,
SampleStateKind.ANY_SAMPLE_STATE,
ViewStateKind.ANY_VIEW_STATE,
InstanceStateKind.ANY_INSTANCE_STATE);
/* We process all the obtained samples for a particular instance */
for (int i = 0; i < _dataSeq.size(); ++i)
{
/* We first check if the sample includes valid data */
SampleInfo info = (SampleInfo)_infoSeq.get(i);
if (info.valid_data)
{
processData((XType)_dataSeq.get(i));
}
}
} catch (RETCODE_NO_DATA noData) {
// No data to process
} finally {
targetReader.return_loan(_dataSeq, _infoSeq);
}
}
/**************************************************************************
* Called for every valid sample received from DDS. */
private void processData(XType instance)
{
double dd = instance.XType.YType.value.double;
# how do I get the values from the other elements of YType (iterate through them?)
}
Hello
You can iterate across a sequence by using its length operation to get the current size of sequence:
And you can access the individual members of the sequence by an array notation:
Take a look at the example that is provided on this community site on how to work with Sequences for help. Also, look at the online html docs for all of the operations you have available when working with sequences.