vbnr
vbnr

Reputation: 307

Generating xsd and Java classes for varying xml child tag - JAXB

I'm wondering if we could generate xsd and corresponding java classes for varying child xml tags. I'm pretty new to xml parsing.

Case : 1

<?xml version="1.0"?>
<SSNExportDocument xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd" Version="0.1" DocumentID="8b6fdfce-9f5f-4110-b6da-c5650f0851c2-1" ExportID="8b6fdfce-9f5f-4110-b6da-c5650f0851c2" JobID="464" RunID="3726980" CreationTime="2019-06-01T20:20:00.417-04:00" StartTime="2019-06-01T14:20:00.000-04:00" EndTime="2019-06-01T20:20:00.000-04:00">
    <MeterData MeterName="CBRE-11459398" UtilDeviceID="11459398" MacID="anonymized">
        <RegisterData StartTime="2019-06-01T14:00:00.000-04:00" EndTime="2019-06-01T14:00:00.000-04:00" NumberReads="1">
            <RegisterRead ReadTime="2019-06-01T14:00:00.000-04:00" GatewayCollectedTime="2019-06-01T19:40:03.116-04:00" RegisterReadSource="REG_SRC_TYPE_EO_CURR_READ" Season="-1">
                <Tier Number="0">
                    <Register Number="1" Summation="9981.2000" SummationUOM="GAL"/>
                </Tier>
            </RegisterRead>
        </RegisterData>
    </MeterData>
    <MeterData MeterName="CBRE-11460365" UtilDeviceID="11460365" MacID="anonymized">
        <RegisterData StartTime="2019-06-01T14:00:00.000-04:00" EndTime="2019-06-01T14:00:00.000-04:00" NumberReads="1">
            <RegisterRead ReadTime="2019-06-01T14:00:00.000-04:00" GatewayCollectedTime="2019-06-01T19:40:03.113-04:00" RegisterReadSource="REG_SRC_TYPE_EO_CURR_READ" Season="-1">
                <Tier Number="0">
                    <Register Number="1" Summation="142104.1000" SummationUOM="GAL"/>
                </Tier>
            </RegisterRead>
        </RegisterData>
    </MeterData>
</SSNExportDocument>

Case: 2

<?xml version="1.0"?>
<SSNExportDocument xmlns="urn:com:ssn:schema:export:SSNExportFormat.xsd" Version="0.1" DocumentID="7b9d9a38-7739-4c67-9fb3-08ff1826d966-2" ExportID="7b9d9a38-7739-4c67-9fb3-08ff1826d966" JobID="465" RunID="2983700" CreationTime="
:2018-12-19T20:20:01.218-05:00" StartTime="2018-12-19T14:20:00.000-05:00" EndTime="2018-12-19T20:20:00.000-05:00">
        <MeterData MeterName="CBRE-11460650" UtilDeviceID="11460650" MacID="00:11:01:ff:fe:00:db:ff">
                <IntervalReadData IntervalLength="60" StartTime="2018-12-19T07:00:00.000-05:00" EndTime="2018-12-19T19:00:00.000-05:00" NumberIntervals="12">
                        <Interval EndTime="2018-12-19T08:00:00.000-05:00" GatewayCollectedTime="2018-12-19T19:40:03.052-05:00" BlockSequenceNumber="0" IntervalSequenceNumber="6636688262981226347">
                                <Reading Channel="1" RawValue="0.0" Value="0" UOM="GAL" BlockEndValue="78582.3000"/>
                        </Interval>
                        <Interval EndTime="2018-12-19T09:00:00.000-05:00" GatewayCollectedTime="2018-12-19T19:40:03.052-05:00" BlockSequenceNumber="0" IntervalSequenceNumber="6636703724863491951">
                                <Reading Channel="1" RawValue="0.0" Value="0" UOM="GAL" BlockEndValue="78582.3000"/>
                        </Interval>
              </IntervalReadData
        </MeterData>
</SSNExportDocument>

In above cases, as you can see contents of MeterData tag are different but MeterData tag remains same.

Upvotes: 0

Views: 54

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

XML documents are often described as "semi-structured", but in truth, they are on a spectrum from highly-structured to highly-unstructured. Conventional programming languages like Java are best at dealing with highly-structured data, and as a result, data binding technologies like JAXB work best at this end of the spectrum. The more you move to highly-unstructured (e.g. HTML) the more difficult JAXB becomes; at that end of the spectrum you should either be using generic tree models like DOM, XOM, or JDOM, or specialist XML processing languages like XSLT and XQuery. The example you've show is in the middle of the spectrum, where you are starting to move out of the JAXB comfort-zone, but where it's still just about viable.

There's another dimension here which is the stability of the structure over time. The more often the schema changes, the more you're moving out of JAXB's comfort zone.

Upvotes: 1

Related Questions