Joseph Lasso Joseph Lasso - 5 days ago 4x
Perl Question

Extracting data from XML tag

I am trying to extract the values from this xml file but can't seem to do it...

<?xml version="1.0" encoding="UTF-8"?>
<personReport xmlns="...">
<person id="person1">
<personId personIdScheme="name of">joe</personId>
<person id="person2">
<personId personIdScheme="name of">sam</personId>

I am successfully able to extract data from other tags (such as header) using:

my $xml = XMLin($xml_file);
my $header = $xml->{header}

I am trying to do the same thing but get the data (joe) out of ...

my $person_type = $xml->{personReport}

Any idea why this isn't working?


should be


If you don't understand why, perhaps you shouldn't be using a module so complex that its author discourages its use.


The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative.

Finding the name of each person using XML::Simple:

# Assumes each person element will have at least one personId child.
# Assumes each personId element will have a personIdScheme attribute.

for my $person (values(%{ $xml->{person} })) {
   my @data_nodes ref($person->{personIdScheme}) eq 'ARRAY'
      ? @{ $person->{personIdScheme} }
      : $person->{personIdScheme};

   my ($name_data_node) = grep { $_->{personIdScheme} eq 'name' } @data_nodes;

   my ($name) = $name_data_node->{content};

Finding the name of each person using XML::LibXML:

for my $person_node ($doc->findnodes('/personReport/person')) {
   my ($name) = $doc->findvalue('personId[@personIdScheme="name of"]', $person_node);