The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Compare two XML files preferably using Python

mabeljuhimabeljuhi Registered User new member
I need to compare two XML files having same structure for any extra lines. Below example provides some details:

File 1:

<Log1> Some text here </Log1>
<Log2>
<Id1> Some text here </Id1>
<Id2> Some text here </Id2>
</Log2>
File2:

<Log1> Some text here </Log1>
<Log2>
<Id1> Some text here </Id1>
<Id2> Some text here </Id2>
</Log2>
<Log3> Some text here </Log3>
I need the diff to identify here that there is one extra tag in File2. Is there an efficient to do this in Python?

Posts

  • ArbitraryDescriptorArbitraryDescriptor Registered User regular
    edited November 2020
    etree findall("Log")?

    Unless your element tags are actually suffixed 1,2,3; but then you just need to iterate through the children and count the tags yourself; which should be pretty straightforward.

    ArbitraryDescriptor on
  • DocDoc Registered User, ClubPA regular
    If you have OSX/Linux, would a simple call to the "diff" command be sufficient? Do you need to match on the tags themselves, and ignore the content, or something? Basically, is there anything here that's really XML specific, or are you just trying to see if a line was added? If needed, there are also utilities to clean up/format an xml file before doing the diff, if that's necessary.

  • ArbitraryDescriptorArbitraryDescriptor Registered User regular
    On the off chance this is homework, given this is the OPs first post, I would offer the following advice instead:

    Instead of soliciting direct advice, you should start here:

    https://www.google.com/search?q=python+diff+xml

    Because, as a discipline, learning to use google is second only to learning to use a debugger; and I would recommend you master both.

    If you can't find someone, somewhere, discussing a problem/challenge similar to the one you're experiencing, then you've either made a brilliant innovation, a fundamental misstep, or a typo.

    Note: Be aware that your teacher definitely knows how to google, and will treat you unkindly if you just copy stuff off of stack overflow

Sign In or Register to comment.