Python Forum
How can I compare Python XML-Files and add missing values from one to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I compare Python XML-Files and add missing values from one to another
#1
I am fairly new to programming with Python and I want to compare values of 2 XML-Files and adding the missing values from XML-File2 to XML-File1

Since I am fairly new I tried to google the basics and got used to the Elementtree Object, I know how to open a file and how to access single elements and traversing the tree. However I do not know how to compare 2 documents with each other.

What I need the script to do is take title and genre from file1, search for them in file2 then compare the child-elements of the active/current <MData> and if they match, add the additional data (that isn't present in file1) to file1. I don't know how to make a proper comparison though. My current guess is that I should add the missing elements to a final xml (see below) and compare the final xml with file2 instead.

At first I thought this was easy but the more I look into it the harder it feels to solve. Any help or any direction is appreciated since I don't really know where to start coding.

Here are the XML Files


File1
<ROOT>
<Meta>
	<Randominfo>Random</Randominfo>
	</Meta>
<MovieList>
	<Movie>
	
		<Movieparameters>
			<Movieparameter genre="Action" Title="Matrix">
			<Length>120 mins</Length>
			<Blu-Ray>Yes</Blu-Ray>
			<Available>No</Available>
			<LentTo>Mr Anderson</LentTo>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0001</ID>
		</Storeparameter>
	</Movie>
	<Movie>
		<Movieparameters>
			<Movieparameter genre="Action" Title="Matrix Reloaded">
			<Length>111 mins</Length>
			<Blu-Ray>Yes</Blu-Ray>
			<Available>No</Available>
			<LentTo>Carrie Ann Moss</LentTo>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0005</ID>
		</Storeparameter>
	</Movie>
	<Movie>
		<Movieparameters>
			<Movieparameter genre="DogMovie" Title="Woofworld">
			<Length>120 mins</Length>
			<Blu-Ray>no</Blu-Ray>
			<Available>No</Available>
			<LentTo>Mr Woofer</LentTo>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0002</ID>
		</Storeparameter>
	</Movie>
	<Movie>
		<Movieparameters>
			<Movieparameter genre="Live Concert" Title="BORK">
			<Length>30 mins</Length>
			<Blu-Ray>Yes</Blu-Ray>
			<Available>Yes</Available>
			<LentTo>None</LentTo></LentTo>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0003</ID>
		</Storeparameter>
	</Movie>
</MovieList>
</ROOT>
File2
<ArrayOfData>
	<Mdata>
		<MovieGenre>Action</MovieGenre>
		<MovieName>Matrix</MovieName>
		<IMDBID>0178544444</IMDBID>
		<Reseller>SmithAndSmith</Reseller>
		<Price>10 Eur</Price>
		<Length>120 mins</Length>
		<Blu-Ray>Yes</Blu-Ray>
	</Mdata>
	<Mdata>
		<MovieGenre>Action</MovieGenre>
		<MovieName>Matrix Reloaded</MovieName>
		<IMDBID>0178555555</IMDBID>
		<Reseller>SmithAndSmith</Reseller>
		<Price>12 Eur</Price>
		<Length>111 mins</Length>
		<Blu-Ray>Yes</Blu-Ray>
	</Mdata>
	<Mdata>
		<MovieGenre>DogMovie</MovieGenre>
		<MovieName>WoofWorld</MovieName>
		<IMDBID>09647852154</IMDBID>
		<Reseller>DogsAndDogs</Reseller>
		<Price>5 Eur</Price>
		<Length>120 mins</Length>
		<Blu-Ray>No</Blu-Ray>
	</Mdata>
		<MovieGenre>Live Concert</MovieGenre>
		<MovieName>BORK</MovieName>
		<IMDBID>1337852</IMDBID>
		<Reseller>DogsAndDogs</Reseller>
		<Price>5 Eur</Price>
		<Length>120 mins</Length>
		<Blu-Ray>Yes</Blu-Ray>
	<Mdata>
	
	</Mdata>
</ArrayOfData>
Final Result (What I want it to look like)
<ROOT>
<Meta>
	<Randominfo>Random</Randominfo>
	</Meta>
<MovieList>
	<Movie>
	
		<Movieparameters>
			<Movieparameter genre="Action" Title="Matrix">
			<Length>120 mins</Length>
			<Blu-Ray>Yes</Blu-Ray>
			<Available>No</Available>
			<LentTo>Mr Anderson</LentTo>
			<IMDBID>0178544444</IMDBID>
			<Reseller>SmithAndSmith</Reseller>
			<Price>10 Eur</Price>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0001</ID>
		</Storeparameter>
	</Movie>
	<Movie>
		<Movieparameters>
			<Movieparameter genre="Action" Title="Matrix Reloaded">
			<Length>111 mins</Length>
			<Blu-Ray>Yes</Blu-Ray>
			<Available>No</Available>
			<LentTo>Carrie Ann Moss</LentTo>
			<IMDBID>0178555555</IMDBID>
			<Reseller>SmithAndSmith</Reseller>
			<Price>12 Eur</Price>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0005</ID>
		</Storeparameter>
	</Movie>
	<Movie>
		<Movieparameters>
			<Movieparameter genre="DogMovie" Title="Woofworld">
			<Length>120 mins</Length>
			<Blu-Ray>no</Blu-Ray>
			<Available>No</Available>
			<LentTo>Mr Woofer</LentTo>
			<IMDBID>09647852154</IMDBID>
			<Reseller>DogsAndDogs</Reseller>
			<Price>5 Eur</Price>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0002</ID>
		</Storeparameter>
	</Movie>
	<Movie>
		<Movieparameters>
			<Movieparameter genre="Live Concert" Title="BORK">
			<Length>30 mins</Length>
			<Blu-Ray>Yes</Blu-Ray>
			<Available>Yes</Available>
			<LentTo>None</LentTo></LentTo>
			<IMDBID>1337852</IMDBID>
			<Reseller>DogsAndDogs</Reseller>
			<Price>5 Eur</Price>
			</Movieparameter>
		</Movieparameters>
		<Storeparameter>
			<ID>0003</ID>
		</Storeparameter>
	</Movie>
</MovieList>
</ROOT>
Reply
#2
you can try https://pypi.org/project/xmldiff/
can't vouch for it as I have never tried it.
Reply
#3
There are also built-in difflib and filecmp
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 533 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 855 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Loop through values and compare edroche3rd 6 684 Oct-18-2023, 04:04 PM
Last Post: edroche3rd
  Trying to compare string values in an if statement israelsattleen 1 541 Jul-08-2023, 03:49 PM
Last Post: deanhystad
  Compare 2 files tslavov 2 947 Feb-12-2023, 10:53 AM
Last Post: ibreeden
  Compare fields from two csv files georgebijum 3 1,366 Apr-25-2022, 11:16 PM
Last Post: Pedroski55
  Compare variable with values in a file paulo79 1 1,104 Apr-22-2022, 03:16 AM
Last Post: Pedroski55
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,475 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Compare two Excel sheets with Python and list diffenrences dmkfon 1 14,613 Oct-09-2021, 03:30 PM
Last Post: Larz60+
  Missing Schema-Python Question Andwconteh 1 2,508 Jun-16-2021, 01:00 PM
Last Post: Andwconteh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020