Hey guys,
I need to parse an XML file and build a record-based output from the data. The problem is that the XML is in a "generic" form, in that it has several levels of nested "node" elements that represent some sort of data structure. I need to build the records dynamically based on the deepest level of the "node" element. Some example XML and expected output are at the bottom.
I am most familiar w/ python's ElementTree, so I'd prefer to use that but I just can't wrap my head around a way to dynamically build the output record based on a dynamic node depth. Also - we can't assume that the nested nodes will be x levels deep, so just hardcoding each level w/ a loop isn't possible. Is there a way to parse the XML and build the output on the fly?
Some Additional Notes:
- The node names are all "node" except the parent and detail info (rate, price, etc)
- The node depth is not static. So - assume further levels than displayed in the sample
- Each "level" can have multiple sub-levels. So - you need to loop on each child "node" to properly build each record.
Any ideas / input would be greatly appreciated.
<root>
<node>101
<node>A
<node>PlanA
<node>default
<rate>100.00</rate>
</node>
<node>alternative
<rate>90.00</rate>
</node>
</node>
</node>
</node>
<node>102
<node>B
<node>PlanZZ
<node>Group 1
<node>default
<rate>100.00</rate>
</node>
<node>alternative
<rate>90.00</rate>
</node>
</node>
<node>Group 2
<node>Suba
<node>default
<rate>1.00</rate>
</node>
<node>alternative
<rate>88.00</rate>
</node>
</node>
<node>Subb
<node>default
<rate>200.00</rate>
</node>
<node>alternative
<rate>4.00</rate>
</node>
</node>
</node>
</node>
</node>
</node>
</root>
The Output would look like this:
SRV SUB PLAN Group SubGrp DefRate AltRate
101 A PlanA 100 90
102 B PlanB Group1 100 90
102 B PlanB Group2 Suba 1 88
102 B PlanB Group2 Subb 200 4