I have a Perl script to convert the XML file below into a hash:
<university>
<name>svu</name>
<location>ravru</location>
<branch>
<electronics>
<student name="xxx" number="12">
<semester number="1"subjects="7" rank="2"/>
</student>
<student name="xxx" number="15">
<semester number="1" subjects="7" rank="10"/>
<semester number="2" subjects="4" rank="1"/>
</student>
<student name="xxx" number="16">
<semester number="1"subjects="7" rank="2"/>
<semester number="2"subjects="4" rank="2"/>
</student>
</electronics>
</branch>
</university>.
.
.
.
.
.
<data>
<student name="msr" number="1" branch="computers" />
<student name="ksr" number="2" branch="electronics" />
<student name="lsr" number="3" branch="EEE" />
<student name="csr" number="4" branch="IT" />
<student name="msr" number="5" branch="MEC" />
<student name="ssr" number="6" branch="computers" />
<student name="msr" number="1" branch="CIV" />
.............................
..............................
.....................
</data>
How can I create a hash table for the data elements, with the name and number as the key and branch is the value in that hash. I need this because some students have the same name and some students have same number.
By using this hash key I have to search in the university node for student if found and print the branch name of each student.
I written some script in XML::Simple but am not able to create a hash.
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use XML::Simple;
my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml", forcearray => [ 'student' , 'semister' ],
KeyAttr => { student => "+Name" } );
print Dumper($data);
by using data dumper I am printing hole xml information. but I need to print only Data Node elements only please help me how to do this.
I would probably write my own
XML::Parserhandler to combine attributes into key values (if that’s something supported byXML::SimpleI couldn’t find it in the docs). This example should get you started:Note that to get this to work I had to clean up your XML a bit by enclosing it in an
<xml>tag and adding some missing spaces:Result: