Reputation: 10584
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
Upvotes: 3
Views: 2306
Reputation: 80186
I think you are re-inventing the wheel. Look into these open source alternatives: Link
Upvotes: 0
Reputation: 2548
Take a look at DiffX (Open Source Java API for XML Diffing). It provides the algorithms for comparing XML documents, and it gives you a nice summary of nodes/attributes/text that were added/deleted/changed (changes are indicated as a delete, followed by an insert). We're using it in a project that I'm currently involved in; it works really well.
Upvotes: 2
Reputation: 328624
Here is a simple algorithm:
Depending on the structure (i.e. if you are sure that the nodes are always the same and only the attributes/text children can be different), you can omit some steps.
Upvotes: 1
Reputation: 10679
Don't use an array.
Use: Map<String, Map<String, String>>
so that:
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)
Upvotes: 4