Skip to content
Advertisement

Comparing XML in java

Here are two XMLs , I am trying to compare and put the respective data in excel sheet.

I have a multidimensional array called provisions.

<xml>
<Item type="ItemHeader" name="Plan Features" id="id_1"></Item>
<Item type="Deductible" name="Deductible" id="a">Calendar Year
 <Item type="Text" name="Individual" id="b">5,000</Item>
 <Item type="Text" name="Family" id="c">10,000</Item>
 <Item type="Text" name="Family Out-of-Network" id="id_4">15,000</Item>
</Item>
<Item lock="|delete|" type="Empty" name="Out-of-Pocket Annual Maximum" id="id_2">
 <Item type="Text" name="Individual" id="d">5,000</Item>
 <Item type="Text" name="Family" id="e">10,000</Item>
</Item>
<Item type="Text" name="Life Time Maximum" id="u">Unlimited</Item>
<Item type="Text" name="Coinsurance" id="f"></Item>
<Item type="Text" name="Office Visits" id="g"></Item>
<Item type="Text" name="Routine Physicals" id="h"></Item>
<Item type="Text" name="Preventive Care" id="m"></Item>
<Item type="Text" name="Physician Services" id="i"></Item>
<Item type="Text" name="Emergency Room Services / Urgent Care" id="j"></Item>
<Item type="Text" name="Hospital Admission Services" id="k"></Item>
<Item type="Text" name="Chiropractic" id="n"></Item>
<Item type="Text" name="Prescription Drugs" id="l"></Item>
<Item type="Text" name="Specialty Drugs" id="o"></Item>
<Item type="Boolean" name="Pre Tax Reduction Available" id="t">false</Item>
<Item type="Boolean" name="Conversion Privilege" id="p">false</Item>
<Item type="ItemHeader" name="Plan Setup" id="id_3"></Item>
<Item type="Termination" name="Benefit Termination Date" id="q">Immediate</Item>
<Item type="Determination" name="Premium Redetermination Date" id="r">Not Applicable</Item>
<Item type="Participation" name="Participation Requirement" id="s"></Item>
</xml>

AND

<xml>
<Item type="ItemHeader" name="Plan Features" id="id_1"></Item>
<Item type="Deductible" name="Deductible" id="a">Calendar Year
 <Item type="Text" name="Individual" id="b">3,000</Item>
 <Item type="Text" name="Family" id="c">6,000</Item>
</Item>
<Item lock="|delete|" type="Empty" name="Out-of-Pocket Annual Maximum" id="id_2">
 <Item type="Text" name="Individual" id="d">5,000</Item>
 <Item type="Text" name="Family" id="e">10,000</Item>
</Item>
<Item type="Text" name="Life Time Maximum" id="u">Unlimited</Item>
<Item type="Text" name="Coinsurance" id="f"></Item>
<Item type="Text" name="Office Visits" id="g"></Item>
<Item type="Text" name="Routine Physicals" id="h"></Item>
<Item type="Text" name="Preventive Care" id="m"></Item>
<Item type="Text" name="Physician Services" id="i"></Item>
<Item type="Text" name="Emergency Room Services / Urgent Care" id="j"></Item>
<Item type="Text" name="Hospital Admission Services" id="k"></Item>
<Item type="Text" name="Chiropractic" id="n"></Item>
<Item type="Text" name="Prescription Drugs" id="l"></Item>
<Item type="Text" name="Specialty Drugs" id="o"></Item>
<Item type="Boolean" name="Pre Tax Reduction Available" id="t">false</Item>
<Item type="Boolean" name="Conversion Privilege" id="p">false</Item>
<Item type="ItemHeader" name="Plan Setup" id="id_3"></Item>
<Item type="Termination" name="Benefit Termination Date" id="q">Immediate</Item>
<Item type="Determination" name="Premium Redetermination Date" id="r">Not Applicable</Item>
<Item type="Participation" name="Participation Requirement" id="s"></Item>
</xml>

Now this XML data is for 2 plans and my provisions array contains

provisions == [[Plan Features,,][Deductible,,][Individual,,]…..]

This is what I have done

for(int j = 0; j < plans.length; j++){
    Vector<String> vr = (Vector<String>) tagidPlan.get(plans[j].getId());
    for(int i = 0; i < vr.size(); i++){
     provisions[i][j+1] = getValues(plans[j],vr.get(i));
    }
}

The problem happens when that extra node of Family Out-of-network comes into picture. This is my final array is

[[Plan Features:, Medical HMO, Medical PPO], [Deductible Year:, Calendar Year, Calendar Year], [Individual:, 5,000, 3,000], [Family:, 10,000, 6,000], [Family Out-of-Network:, 15,000, null], [Out-of-Pocket Annual Maximum:, null, 5,000], [Individual:, 5,000, 10,000], [Family:, 10,000, Unlimited], [Life Time Maximum:, Unlimited, ], [Coinsurance:, , ], [Office Visits:, , ], [Routine Physicals:, , ], [Preventive Care:, , ], [Physician Services:, , ], [Emergency Room Services / Urgent Care:, , ], [Hospital Admission Services:, , ], [Chiropractic:, , ], [Prescription Drugs:, , ], [Specialty Drugs:, , false], [Pre Tax Reduction Available:, false, false], [Conversion Privilege:, false, ], [Plan Setup:, , Immediate], [Benefit Termination Date:, Immediate, Not Applicable], [Premium Redetermination Date:, Not Applicable, ], [Participation Requirement:, , null]]

I want to get right values into corresponding array element.

More code can be seen here pastie.org/1308625

Advertisement

Answer

Don’t use an array.

Use: Map<String, Map<String, String>>

so that:

  • the first String (key to the outer map) is the feature name (e.g. “Life Time Maximum”)
  • the second String (key to the inner map) is the plan name (there don’t seem to be any actual plan names in your XML documents so “Plan1” and “Plan2” could suffice)
  • the third String (value to the inner map) should be the value for that particular feature in that particular plan (e.g. “Unlimited” for “Life Time Maximum” in “Plan1”)

You could have:

{ Life Time Maximum: { Plan1: Unlimited, Plan2: Unlimited } }
{ Family Out-Of-Network: { Plan1: 15,000 } }

as, unlike an array, the number of entries for each feature doesn’t have to be fixed (different features can have different numbers of entries)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement