Hi I had written a generic code in Java that parses XML input file without knowing its structure and outputs value in comma separated value. So lets say I have following in my XML document:
<Employee>
<Name>XYZ</Name>
<Id>123</Id>
<Address>
<Office_Address>office address here</Office_Address>
</Address>
</Employee>
So now my Java code parses above xml file into comma separated value as:
Employee (File 1): Name , ID
Address (File 2): Office_Address
That is for each nested element its output a new csv file having columns inside it equals to its child nodes.
So this is working fine but now problem is : Lets I am having same above mentioned XML file as:
<Employee>
<Name>XYZ</Name>
<Id>123</Id>
<Address/>
</Employee>
So in this case when my generic Java code process this file it outputs as:
Employee (File 1) : Name, Id, Address
So instead of two output file I am getting one and file 1 has sometimes 3 entries instead of 2. This happens because Address element is present sometime as nested and some time as flat. So when it is nested Java code creates a new comma separated corresponding to it but when it is not nested than it outputs just one file.
I can solve this problem by hard coding the logic for this element. But I do not want to do that as than there will be no point of my Java generic XML parsing code.
So my question is that any way in which we can figure out that an element in an XML files generating from same sources may be present as nested and sometime as flat. Use of XSD or any other way. I tried researching many things but not able to figure out anything.
Thanks in advance and hoping to get solution or few good suggestions.