Comparing XML in java
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.
- Calendar Year
- 5,000
- 10,000
- 15,000
-
- 5,000
- 10,000
- Unlimited
- false
- false
- Immediate
- Not Applicable
AND
- Calendar Year
- 3,000
- 6,000
-
- 5,000
- 10,000
- Unlimited
- false
- false
- Immediate
- Not Applicable
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 vr = (Vector) 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
Answer by Pangea for Comparing XML in java
I think you are re-inventing the wheel. Look into these open source alternatives: http://blogs.msdn.com/b/dmahugh/archive/2008/06/18/open-xml-diff-tools.aspx
Answer by Martijn Verburg for Comparing XML in java
Also see Best way to compare 2 XML documents in Java
Answer by barrowc for Comparing XML in java
Don't use an array.
Use: Map
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)
Answer by Aaron Digulla for Comparing XML in java
Here is a simple algorithm:
- Load the data into two DOM models
- Iterate over all nodes in the first model, depth first (i.e. first work on children and then the parent nodes)
- Try to find the same node in the second model. If you can't find one -> You found a node that only exists in document 1
- Compare all attributes between the two nodes. Add any differences to your excel sheet
- Remove the node in the second document unless it has children
- Iterate over all nodes in the second model, depth first
- Try to find the same node in the first model. If you can't find one -> You found a node that only exists in document 2
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.
Answer by Jeff Knecht for Comparing XML in java
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.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment