Comparing XML files

Discussion in 'Perl Misc' started by Rumpa, Aug 9, 2005.

  1. Rumpa

    Rumpa Guest

    Hi,

    I have an XML file which has holds actual result. I have another XML file which has the expected result. I need to compare these two and see if they
    are the same. Whitspaces and placement of the elements might be diferent. What is the best way to handle this?

    Example:
    Got this result:
    <store>
    <book>
    <name>XYZ</name>
    <author>ABC</author>
    </book>
    </store>


    Expected result:
    <store>
    <book>
    <author> ABC </author>
    <name> XYZ </name>
    </book>
    </store>

    Thanks,
    Rumpa
    Rumpa, Aug 9, 2005
    #1
    1. Advertising

  2. Rumpa

    Guest

    Rumpa wrote:
    > Hi,
    >
    > I have an XML file which has holds actual result. I have another XML file which has the expected result. I need to compare these two and see if they
    > are the same. Whitspaces and placement of the elements might be diferent. What is the best way to handle this?
    >
    > Example:
    > Got this result:
    > <store>
    > <book>
    > <name>XYZ</name>
    > <author>ABC</author>
    > </book>
    > </store>
    >
    >
    > Expected result:
    > <store>
    > <book>
    > <author> ABC </author>
    > <name> XYZ </name>
    > </book>
    > </store>




    Use one of the XML parsers found
    in the CPAN to transform data into
    a suitable structure.

    Compare as needed. For example:

    use XML::Simple;
    use Test::More tests => 1;
    use Test::Deep;

    my $actual =
    '<store>
    <book>
    <name>XYZ</name>
    <author>ABC</author>
    </book>
    </store>';

    my $expected =
    '<store>
    <book>
    <author> ABC </author>
    <name> XYZ </name>
    </book>
    </store>';

    my $a_ = XMLin($actual, NormaliseSpace => 2 );
    my $e_ = XMLin($expected, NormaliseSpace => 2 );

    cmp_deeply(
    $a_,
    $e_,
    "XML::Simple hashes"
    );

    --
    Hope this helps,
    Steven
    , Aug 9, 2005
    #2
    1. Advertising

  3. Rumpa

    Bart Lateur Guest

    Rumpa wrote:

    >I have an XML file which has holds actual result. I have another XML file
    >which has the expected result. I need to compare these two and see if they
    >are the same.


    Take a look at the module XML::Diff. It might do what you want.

    --
    Bart.
    Bart Lateur, Aug 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe
    Replies:
    0
    Views:
    373
  2. Joe
    Replies:
    0
    Views:
    401
  3. Corno
    Replies:
    8
    Views:
    1,894
  4. kj7ny
    Replies:
    2
    Views:
    299
    Yu-Xi Lim
    Dec 29, 2006
  5. junnuthala

    Comparing huge XML Files

    junnuthala, Feb 23, 2005, in forum: Perl Misc
    Replies:
    10
    Views:
    332
    junnuthala
    Feb 25, 2005
Loading...

Share This Page