Solved

Compare contents of 2 xml files

Posted on 2001-06-14
14
214 Views
Last Modified: 2013-11-23
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
Comment
Question by:d97adka
  • 4
  • 4
  • 2
  • +4
14 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 6191326
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
 

Author Comment

by:d97adka
ID: 6191414
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
 
LVL 3

Expert Comment

by:Kobold
ID: 6191766
Is your structure always the same? Do you want to enumerate only the "data" difference or the xml tag difference?
0
 

Author Comment

by:d97adka
ID: 6191822
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
 

Author Comment

by:d97adka
ID: 6191826
I need data differance. Including attribute values.
0
 
LVL 3

Expert Comment

by:Kobold
ID: 6192158
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
 

Expert Comment

by:makrand
ID: 6193587
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:tonus
ID: 6193887
Here is the URL http://www.geherbert.de/ where you can download the compare utility.
0
 

Author Comment

by:d97adka
ID: 6194546
I need some code example not a comment.
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6196858
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6196865
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
 
LVL 3

Accepted Solution

by:
superschlonz earned 300 total points
ID: 6196868
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
 
LVL 3

Expert Comment

by:superschlonz
ID: 6196874
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
 
LVL 2

Expert Comment

by:iDeb
ID: 6196943
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now