xml string compare

M

modhak

Hi All

I have 2 xml strings as shown below. (Each set is in a string)

<set>
<item table_name='tname1' column_name='colname1'>34</item>
<item table_name='tname1' column_name='colname2'>35</item>
<item table_name='tname3' column_name='colname1'>36</item>
<item table_name='tname1' column_name='colname4'>37</item>
<item table_name='tname2' column_name='colname1'>38</item>
<item table_name='tname1' column_name='colname5'>39</item>
</set>

<set>
<item table_name='tname1' column_name='colname1'>34</item>
<item table_name='tname1' column_name='colname2'>36</item>
<item table_name='tname3' column_name='colname1'>38</item>
<item table_name='tname1' column_name='colname4'>mmm</item>
<item table_name='tname2' column_name='colname1'>38</item>
<item table_name='tname1' column_name='colname5'>50</item>
</set>

I have a table with 3 fields:
TableName, ColumnName, Inc_Exc
tname1, column1, I
tname1, column2, E
tname3, column1, I

I need to write to a file that...

The two sets are identical if all fields in the table that has inc_exc
flag set to I are same.

In the above example I have to say it is different because tname3 and
column name1 has an include flag but has different values.
Please note that the sets would have been similar if tname3 and
colname1 has same value. Even though other values may differ.

I do not have to take values for which there in entry in my database
table.

Any ideas how to do this....

Code sample is appreciated, however if basic idea is given I can code
it myself.

Thanks much
 
R

Roland de Ruiter

Hi All

I have 2 xml strings as shown below. (Each set is in a string)

<set>
<item table_name='tname1' column_name='colname1'>34</item>
<item table_name='tname1' column_name='colname2'>35</item>
<item table_name='tname3' column_name='colname1'>36</item>
<item table_name='tname1' column_name='colname4'>37</item>
<item table_name='tname2' column_name='colname1'>38</item>
<item table_name='tname1' column_name='colname5'>39</item>
</set>

<set>
<item table_name='tname1' column_name='colname1'>34</item>
<item table_name='tname1' column_name='colname2'>36</item>
<item table_name='tname3' column_name='colname1'>38</item>
<item table_name='tname1' column_name='colname4'>mmm</item>
<item table_name='tname2' column_name='colname1'>38</item>
<item table_name='tname1' column_name='colname5'>50</item>
</set>

I have a table with 3 fields:
TableName, ColumnName, Inc_Exc
tname1, column1, I
tname1, column2, E
tname3, column1, I

I need to write to a file that...

The two sets are identical if all fields in the table that has inc_exc
flag set to I are same.

In the above example I have to say it is different because tname3 and
column name1 has an include flag but has different values.
Please note that the sets would have been similar if tname3 and
colname1 has same value. Even though other values may differ.

I do not have to take values for which there in entry in my database
table.

Any ideas how to do this....

Code sample is appreciated, however if basic idea is given I can code
it myself.

Thanks much
Something like this:


public static boolean equal(ItemSet set1, ItemSet set2,
Constraints constraints) throws
CannotDetermineEqualityException {

Set<TableColumn> constrainedTableColumns = constraints
.getAllTableColumns();

// Check if all table+columns in set1 have a constraint
if (!constrainedTableColumns.containsAll(
set1.getTableColumnSet())) {
Set<TableColumn> unknownTableColumns
= set1.getTableColumnSet();
unknownTableColumns.removeAll(constrainedTableColumns);
StringBuilder buf = new StringBuilder("No constraints for");
for (TableColumn pair : unknownTableColumns) {
buf.append(' ').append(pair);
}
throw new CannotDetermineEqualityException(buf.toString());
}

// Check if all table+columns in set2 have a constraint
if (!constrainedTableColumns.containsAll(
set2.getTableColumnSet())) {
Set<TableColumn> unknownTableColumns
= set2.getTableColumnSet();
unknownTableColumns.removeAll(constrainedTableColumns);
StringBuilder buf = new StringBuilder("No constraints for");
for (TableColumn pair : unknownTableColumns) {
buf.append(' ').append(pair);
}
throw new CannotDetermineEqualityException(buf.toString());
}

// Now we are interested in the included table-columns
Set<TableColumn> includedTableColumns = constraints
.getIncludedTableColumns();

// For each included table+column, check if item in set1 equals
item in set2
for (TableColumn tableColumn : includedTableColumns) {
// Allowing more than 1 item for table+column in set
// And if there are multiple items of the same
table+column, ...
List<Item> items1 = set1.findAllItemsOf(tableColumn);
List<Item> items2 = set2.findAllItemsOf(tableColumn);

// ... then both sets must have the same number of items...
if (items1.size() != items2.size()) {
return false;
}

// ... and they must be equal in the same order
Iterator<Item> i1 = items1.iterator();
Iterator<Item> i2 = items2.iterator();
for (; i1.hasNext() && i2.hasNext();) {
Item item1 = i1.next();
Item item2 = i2.next();
if (!item1.equals(item2)) {
System.out.println(item1 + " not equal to " + item2);
return false;
}
}
}
return true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top