Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:20:35+00:00 2026-05-26T16:20:35+00:00

I have a Perl script to convert the XML file below into a hash:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:20:36+00:00Added an answer on May 26, 2026 at 4:20 pm

    I would probably write my own XML::Parser handler to combine attributes into key values (if that’s something supported by XML::Simple I couldn’t find it in the docs). This example should get you started:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use XML::Parser;
    use Data::Dumper;
    
    my %hash;
    
    sub tag_start { my ($expat, $tagname) = (shift, shift);
        # attributes are now in @_
        my %a = grep { $_=$_=>shift } @_; # attribute hash for this tag
        my $context = join('/',$expat->context()) || '';
    
        if ($context eq 'xml/data') {
            if ($tagname eq 'student') {
                push @{($hash{"$a{name}:$a{number}"}||=[])}, $a{branch};
            }
        } elsif ($context eq ...) {
            ...
        }
    }
    my $p = new XML::Parser(Handlers => { Start=>\&tag_start });
    $p->parsefile('file.xml');
    print Dumper \%hash;
    

    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:

    <xml>
        <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>
    </xml>
    

    Result:

    $VAR1 = {
              'ksr:2' => [
                         'electronics'
                       ],
              'msr:1' => [
                         'computers',
                         'CIV'
                       ],
              'csr:4' => [
                         'IT'
                       ],
              'ssr:6' => [
                         'computers'
                       ],
              'msr:5' => [
                         'MEC'
                       ],
              'lsr:3' => [
                         'EEE'
                       ]
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert a shell script into a Perl script? I have a
I have a Perl script that uses WWW::Mechanize to read from a file and
I have Perl script which appends a new line to the existing file every
I have a Perl command-line script that I want to convert to a rich.
I have a string containing fully formatted XML data, created using a Perl script.
I have Perl script and need to determine the full path and filename of
I have a Perl script that I'm attempting to set up using Perl Threads
I have a Perl script where I maintain a very simple cache using a
I have this Perl script with many defined constants of configuration files. For example:
I have a Perl script that sets up variables near the top for directories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.