0

I have an XSD definition as :

<beanio xmlns="http://www.beanio.org/2012/03">
    <stream name="udraccess" format="xml">
    <parser class="org.beanio.stream.xml.XmlRecordParserFactory" />
.
..lines
..of
..XML code
                    <segment name="aggregationDimensions" class="java.util.HashMap" collection="list" minOccurs="0" maxOccurs="unbounded">
                        <segment name="aggregationDimension" class="java.util.HashMap" collection="list" minOccurs="0" maxOccurs="unbounded">
                            <field name="aggregationDimensionType" xmlType="element" minOccurs="0"/>
                            <field name="aggregationThresholdDimensionUnitType" xmlType="element" minOccurs="0" />
                            <field name="aggregationDimensionValue" xmlType="element" minOccurs="0"/>
                            <segment name="aggregationDimensionAverageInfo" class="java.util.HashMap" collection="list" minOccurs="0" maxOccurs="unbounded">
                                <field name="averageValueTotal" xmlType="element" minOccurs="0"/>
                                <field name="averageValueCount" xmlType="element"  minOccurs="0"/>
                                <field name="averageValuePerType" xmlType="element" minOccurs="0"/>
                            </segment>
                        </segment>
                    </segment>

Now in this particular segment: aggregationDimensionAverageInfo, I need that if this value is not present in the input XML, the output XML file should not have that field at all... Currently I am getting the field as an empty array, I want that it should be skipped completely if missing:

                    "aggregationDimensions": [
                        {
                            "aggregationDimension": [
                                {
                                    "aggregationDimensionAverageInfo": [],
                                    "aggregationDimensionType": "106",
                                    "aggregationDimensionValue": "15:"
                                }
                            ]
                        }
                    ],

If that field is present in the input XML, I get it alright as below:

                    "aggregationDimensions": [
                        {
                            "aggregationDimension": [
                                {
                                    "aggregationDimensionAverageInfo": [
                                        {
                                            "averageValuePerType": "50",
                                            "averageValueTotal": "50",
                                            "averageValueCount": "1"
                                        }
                                    ],
                                    "aggregationDimensionType": "106",
                                    "aggregationDimensionValue": "0:15"
                                }
                            ]
                        }
                    ],

I need help with the scenario where its missing from the input XML.

1 Answers1

0

You need to set the lazy attribute on the segment to true.

<segment name="aggregationDimensionAverageInfo" lazy="true" class="java.util.HashMap" collection="list" minOccurs="0" maxOccurs="unbounded" >
    <field name="averageValueTotal" xmlType="element" minOccurs="0"/>
    <field name="averageValueCount" xmlType="element"  minOccurs="0"/>
    <field name="averageValuePerType" xmlType="element" minOccurs="0"/>
</segment>

The reference guide state the following for the lazy attribute on a segment:

If set to true, the class or collection bound to this segment will only be instantiated if at least one child attribute is not null or the empty String. Defaults to false. This functionality differs from minOccurs in that the fields may still exist in the input stream.

nicoschl
  • 2,346
  • 1
  • 17
  • 17