Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Compare contents of 2 xml files

Hi,
I wonder how to compare contents of 2 xml files and put the differance in a new xml file.
For ex

xmlfile1
---------
<AgentName>
  <CO ID="001" Name="SL_001" Date="06/06/2001">
    <Quantity Class="QuantityUOM" Quantity="480" UOM="PC" />
    <Resource ID="58907146" />
  </CO>
  <CO ID="002" Name="SL_002" Date="06/06/2001">
    <Quantity Class="QuantityUOM" Quantity="36" UOM="PC" />
    <Resource ID="58907294" />
  </CO>
  <CO ID="003" Name="SL_003" Date="13/06/2001">
    <Quantity Class="QuantityUOM" Quantity="660" UOM="PC" />
    <Resource ID="58907146" />
  </CO>
</AgentName>

xmlfile2
---------
<AgentName>
  <CO ID="001" Name="SL_001" Date="06/06/2001">
    <Quantity Class="QuantityUOM" Quantity="480" UOM="PC"/>
    <Resource ID="58907146"/>
  </CO>
  <CO ID="002" Name="SL_002" Date="06/08/2001">
    <Quantity Class="QuantityUOM" Quantity="36" UOM="PC"/>
    <Resource ID="58907294"/>
  </CO>
  <CO ID="003" Name="SL_003" Date="13/06/2001">
    <Quantity Class="QuantityUOM" Quantity="660" UOM="PC"/>
    <Resource ID="58907146"/>
  </CO>
  <CO ID="004" Name="SL_004" Date="12/06/2001">
    <Quantity Class="QuantityUOM" Quantity="80" UOM="PC"/>
    <Resource ID="58907294"/>
  </CO>
</AgentName>

I know that I should parse it and compare elements and compare. Is there any already written function
that do this. I tried to use SAX and to get the first CO tag and its attributes and than to start second parser and find the same element, but it wouldn't work. When the first parser starts it parses until end. So I couldn't make it work with two parsers. Anyone having the solution to this.

!!!I need source code and it's worth 300 points.!!!
I have tried for 3 days and failed.
Thanks!!!
0
d97adka
Asked:
d97adka
  • 4
  • 4
  • 2
  • +4
1 Solution
 
ozymandiasCommented:
What differences do you want to find ?
Do you want to find

a) any <CO> elements that are in file1 but not in file two
b) vice versa
c) any <CO> elements that are in both files but whoes details are different
d) a combination of the above ?
0
 
d97adkaAuthor Commented:
I want to find if there is new COs or if
some attribute is changed for any of CO in second xml file
compared with the first one.
0
 
KoboldCommented:
Is your structure always the same? Do you want to enumerate only the "data" difference or the xml tag difference?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
d97adkaAuthor Commented:
I want to find if there is new COs or if
some attribute is changed for any of CO in second xml file
compared with the first one.
0
 
d97adkaAuthor Commented:
I need data differance. Including attribute values.
0
 
KoboldCommented:
You can use a DOM parser and compare the XML file with some small operation. I think that there is no automatic way to do what you want!

Try Xerces at http://java.apache.org
0
 
makrandCommented:
I had a similar kind of scenario too, of finding the differences between the xml files.
I used the JDOM parser for that, then just looped in a recursive method for find out the difference. And then added the element to a list if it was different than old one.
With JDOM it becomes a very simple way.

regards
mak
0
 
tonusCommented:
Here is the URL http://www.geherbert.de/ where you can download the compare utility.
0
 
d97adkaAuthor Commented:
I need some code example not a comment.
0
 
superschlonzCommented:
0
 
superschlonzCommented:
Seems this was too big, I'll try again.

I have some code which still doesn't do what you want, but it shows how you could
do it.

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.xml.sax.*;
import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;

public class Agents
{
     Hashtable agents = new Hashtable();
     String fileName;

     public Agents( String file, Element elem )
     {
          fileName = file;
          if( !elem.getTagName().equals("AgentName") )
          {
               throw new IllegalArgumentException( "tag AgentName expected" );
          }
          NodeList nodes = elem.getChildNodes();
          for( int i=0; i<nodes.getLength(); ++i )
          {
               Node node = (Node)nodes.item(i);
               if( node instanceof Element )
               {
                    Agent agent = new Agent( (Element)node );
                    agents.put( agent.getName(), agent );
               }
          }
     }

     public static void main( String args[] )
     {
          if( args.length != 2 )
          {
               System.out.println( "please give me 2 XML files" );
               System.exit(1);
          }
          Agents a[] = new Agents[2];
          for( int i=0; i<2; ++i )
          {
               try {
                    DOMParser parser = new DOMParser();
                    FileInputStream is = new FileInputStream( args[i] );
                    InputSource isrc = new InputSource( is );
                    parser.parse( isrc );
                    Document doc = parser.getDocument();
                    a[i] = new Agents( args[i], (Element)doc.getDocumentElement() );
               } catch( Exception ex ) {
                    System.out.println( "error reading file " + args[i] );
                    ex.printStackTrace();
                    System.exit(1);
               }
          }
          compare( a[0], a[1] );
     }

     public static void compare( Agents a1, Agents a2 )
     {
          Enumeration names = a1.agents.keys();
          while( names.hasMoreElements() )
          {
               String name = (String)names.nextElement();
               Agent ag1 = (Agent)a1.agents.get( name );
               Agent ag2 = (Agent)a2.agents.get( name );
               if( ag2 == null )
               {
                    System.out.println( "agent " + ag1.getName() + " exists only in file " + a1.fileName );
               }
               else
               {
                    System.out.println( "agent " + ag1.getName() + " exists in both files" );
               }
          }
          names = a2.agents.keys();
          while( names.hasMoreElements() )
          {
               String name = (String)names.nextElement();
               Agent ag2 = (Agent)a2.agents.get( name );
               if( !a1.agents.containsKey( name ) )
               {
                    System.out.println( "agent " + ag2.getName() + " exists only in file " + a2.fileName );
               }
          }
     }
}
0
 
superschlonzCommented:
And the second class:

import org.w3c.dom.*;

public class Agent
{
     int id;
     String name;
     String date;

     public Agent( Element elem )
     {
          if( !elem.getTagName().equals("CO") )
          {
               throw new IllegalArgumentException( "tag CO expected" );
          }
          NamedNodeMap map = elem.getAttributes();
          for( int i=0; i<map.getLength(); ++i )
          {
               Attr a = (Attr)map.item(i);
               //System.out.println( a.getName() + ": " + a.getValue() );
               if( a.getName().equals( "ID" ) )
               {
                    id = Integer.parseInt( a.getValue() );
               }
               else if( a.getName().equals( "Name" ) )
               {
                    name = a.getValue();
               }
               else if( a.getName().equals( "Date" ) )
               {
                    date = a.getValue();
               }
               else
               {
                    throw new IllegalArgumentException( "illegal attribute " + a.getName() );
               }
          }
     }

     public String getName()
     {
          return name;
     }
}
0
 
superschlonzCommented:
You would have to some code for Quantity and Resource and
for comparing these 'Agents' at the line which joust outputs
'exists in both files'.
0
 
iDebCommented:
This may give you the answers to what you're looking for:-
http://www-106.ibm.com/developerworks/xml/library/x-diff/index.html?dwzone=xml
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now