Need help in xml

K

Kirt

i have two xml documemts of type

<job>
<jobname>test</jobname>
<jobdate>2006-12-12</jobdate>
<jobtime>12:12:12</jobtime>
<directory>
<dirname>/home/</dirname>
<file>
<name>test2</name>
<Modified time>12:12:12</Modified time>
</file>
</directory>
<directory>
<dirname>/home/test</dirname>
<file>
<name>test3</name>
<Modified time>12:12:12</Modified time>
</file>
</directory>
</job>

i have to compare 2 similar xml document and get the add, changed and
deleted files.and write it into acd.xml file.
can u help me with the python code for this. I am using SAX.
 
U

uche.ogbuji

Kirt said:
i have two xml documemts of type

<job>
<jobname>test</jobname>
<jobdate>2006-12-12</jobdate>
<jobtime>12:12:12</jobtime>
<directory>
<dirname>/home/</dirname>
<file>
<name>test2</name>
<Modified time>12:12:12</Modified time>
</file>
</directory>
<directory>
<dirname>/home/test</dirname>
<file>
<name>test3</name>
<Modified time>12:12:12</Modified time>
</file>
</directory>
</job>

i have to compare 2 similar xml document and get the add, changed and
deleted files.and write it into acd.xml file.
can u help me with the python code for this. I am using SAX.

Use the right tool and such problems tend to become much simpler.

http://www.logilab.org/projects/xmldiff
 
D

Dennis Lee Bieber

i have two xml documemts of type
said:
i have to compare 2 similar xml document and get the add, changed and
deleted files.and write it into acd.xml file.
can u help me with the python code for this. I am using SAX.

Looks like you've just encountered the big flaw with XML... It is
not a "database"! XML may be nice for transferring data from one program
to another, but for tasks like the one you describe, that data needs to
be transformed into some more usable format...

If I were on something more like an old mainframe, I'd convert it
from XML to a flat file (one file for each input XML) of the form

/home/test2 12:12:12
/home/test/test3 12:12:12

Sort each file (just in case the directory/file names are not in
order)

Run the system DIFF utility on the files

Parse the output of DIFF.

That's a first cut... Of course, depending upon the size of the
data, you might be able to just create a list (for each XML) of tuples

[ ("/home/test2", "12:12:12"),
("home/test/test3", "12:12:12") ]

Sort the lists

Do a variation of a "sort/merge" operation.

#assume ol is the "old" list, nl is the "new" list
oitem = ol.pop(0)
nitem = nl.pop(0)
while True:
if oitem[0] < nitem[0]:
print "Deleted:", oitem[0]
oitem = ol.pop(0)
elif oitem[0] > nitem[0]:
print "Added:", nitem[0]
nitem = nl.pop(0)
else: #names are equal
if oitem[1] != nitem[1]:
print "Modified:", oitem[0], nitem[1]
else:
print "Unchanged:", oitem[0]
oitem = ol.pop(0)
nitem = nl.pop(0)

NOTE: I have not shown code to handle the end of list condition; such
would have to be added above to break out of the loop, and to list all
remaining records in the longer list.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
K

Kirt

Thanx Dennis Lee Bieber for ur suggestion.


After following ur suggestion i am getting 3 list after reading and
comparing the 2 xml document.
<directory>+<filename>,<modified time>
adds ['/home/moq/buc/2+add.py,200606281354\r \n',
'/home/moq/buc/1+add.py,200607031215\r \n']

Modified ['/home/moq/buc/2+mod2.py,200607111031\r \n',
'/home/moq/buc/1+mod.py,200607111031\r \n']

deletes ['/home/moq/buc/2+del.py,200606281354\r \n',
'/home/moq/buc/1+del.py,200607031215\r \n']

I have to put these in to an xml format of type
<changes>
<added>
<directory>
<dirname>/home/moq/buc/2</dirname>
<file>
<name>add.py</name>
<time>200606281354</time>
</file>
</directory>
<directory>
<dirname>/home/moq/buc/1</dirname>
<file>
<name>add</name>
<time>200607031215</time>
</file>
</directory>
</added>
</changes>

Similarly for Modified and deleted.
can someone help me with the code for this. Thanx
 

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

Staff online

Members online

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,700
Latest member
jody1922

Latest Threads

Top