Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

xml string compare

Thread view: 
modhak@gmail.com - 21 Aug 2006 08:39 GMT
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
Roland de Ruiter - 21 Aug 2006 16:23 GMT
> Hi All
>
[quoted text clipped - 43 lines]
>
> 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;
    }

Signature

Regards,

Roland

modhak@gmail.com - 21 Aug 2006 23:42 GMT
Thanks very much


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.