We help IT Professionals succeed at work.

Perl processing of an XML file

fabiano petrone
on
Hello,
an xml file contains user records like:

<user>
<account_type>EXTERNAL</account_type>
<primary_id>BRMNTN62M16A944E</primary_id>
<first_name>John</first_name>
<last_name>Doe</last_name>

...other fields...

<user_identifier>
<id_type desc="Additional">02</id_type>
<value>john.doe@mymail.com</value>
</user_identifier>

...other fields...

</user>

Open in new window

it happens that some records have the SAME <primary_id> but the <value> of the <user_identifier> of <type> 02 (see above) have a value different than "<first_name>.<last_name>@mymail.com"
is it possible individuating & suppressing all and only these <user> records?
Thanks a lot,
Fabiano
Comment
Watch Question

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Same <primary_id> as what? Same as "BRMNTN62M16A944E"
What characters are allowed <first_name> and <last_name>?

Author

Commented:
Hi, ozo
yes: same as "BRMNTN62M16A944E".
i.e. there're 2 records with
1) same <primary_id>BRMNTN62M16A944E</primary_id>

2) same first & second, say:
<first_name>John</first_name>
<last_name>Doe</last_name>

3) but different email, say:
<user_identifier>
<id_type desc="Additional">02</id_type>
<value>john.doe@mymail.com</value>
</user_identifier>

for the first record and

<user_identifier>
<id_type desc="Additional">02</id_type>
<value>something.else@mymail.com</value>
</user_identifier>

I want to keep only the first <user>...</user> record and delete the second
Thanks a lot,
Fabiano

Author

Commented:
Hi, ozo
to make the things more clearer (I hope) I've attached a sample file with 2 <user> records.
They have the same value for the fields:
<primary_id>BRMNTN62M16A944E</primary_id>
<first_name>john</first_name>
<last_name>doe</last_name>

but different value in the user_identifier field.
for the first record:
<user_identifier>
<id_type desc="Additional">02</id_type>
<value>john.doe@uniud.it</value>
</user_identifier>

and for the second one:

<user_identifier>
<id_type desc="Additional">02</id_type>
<value>someOther.thing@uniud.it</value>
</user_identifier>

I want to keep only the first <user> record and delete the second.
Thanks,
Fabiano
sample.xml
You need to parse your XML with a Perl module which gives you access to all its elements, in a hierarchy that you can use to find whatever you want and delete it.   I generally use XML::LibXML for this.  Here's a little bit of code that may help you get going; XML::LibXML and its related modules (XML::LibXML::Node and XML::LibXML::Element are the ones you'll use most) will do everything you want.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use XML::LibXML;

my $dom = XML::LibXML->load_xml(location => 'sample.xml');

my @primary_ids = $dom->findnodes('/users/user/primary_id');

my $previous_id = '';
foreach my $id_elt ( @primary_ids ){
    my $this_id = $id_elt->to_literal();
    if ( $this_id eq $previous_id ){
	my @user_identifiers = $id_elt->parentNode()->findnodes('./user_identifiers/user_identifier');
	print "User identifiers for $this_id: \n";
	print "-----------------\n$_\n" for @user_identifiers;
        print "Here you do other things as required\n";
    }
    else {
	$previous_id = $this_id;
    }
}

Open in new window

Author

Commented:
Hi, Thanks a lot for the reply
is this work possible with the XML::Simple module?
Thanks,
Fabiano
You could try, but i personally wouldn't recommend it.  XML::Simple makes assumptions about the way that the XML data is represented by variables--arrays, hashes and so forth--which may not be the best way of doing it from the programming point of view.  There are parameters that you can pass which change that, but the way the arrays and hashes are created has a tendency to change with the structure of the data, so that your program may fail at some later stage because perfectly valid XML is structured in a new way.  The CPAN entry for XML::Simple even says this: "The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces"

If you know XML::Simple then this page may be of interest.

Author

Commented:
Hi,
Thanks for the reply.
I use W7 with activeperl 5.24.0 and on the ppm I can find the XML::Simple and XML::Parser modules, but sadly not the XML::LibXML module
That's frustrating.  I used to use ActivePerl (before I switched to Linux) and thought they were pretty good.  People have managed to install XML::LibXML (see here) but it doesn't sound straightforward.

There are lots of other XML parsing packages that will probably do; it's just that I don't have experience of it. I do know that doing what you want to do with XML::Simple is not a good idea!

Author

Commented:
Hi, Henry
Thanks: I'll follow your suggestions
Thanks,
Fabiano