Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi, I have the xml file file this, perl script to create hash

<university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="mikky" number="12"> <semester number="1"subjects="7" rank="2"/> </student> <student name="vijju" number="15"> <semester number="1" subjects="7" rank="10"/> <semester number="2" subjects="4" rank="1"/> </student> <student name="shyam" number="16"> <semester number="1"subjects="7" rank="2"/> <semester number="2"subjects="4" rank="2"/> </student> </electronics> </branch> </university>. . . . . . <data> <student name="mikky" number="1" branch="computers" /> <student name="vijju" number="2" branch="electronics" /> <student name="shyam" 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 to create hash table for data elements. name and number as the key and branch is the value in that hash. why I need like this means some students have same name and some students have same number. by using this hash keys I have to search in the university node for student if found and print branch name of each student.

I written some script in XML::Simple but I am not able to create 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' , 'semist +er'], + KeyAttr=>{student=>"+Name"}); print Dumper($data);
its printing like this by using data dumper.
$var1={ 'data'=>{ 'student'=>[ { 'name'=>'mikky', 'number'=>'12', 'branch'=>'computers', }, { 'name'=>'vijju', 'number'=>'15', 'branch'=>'electronics', }, { 'name'=>'shyam', 'number'=>'16', 'branch'=>'EEE', }, ], 'university'=>{ 'name'=>'svu', 'location'=>'ravru', 'branch'=>{ 'electronics'=>{ 'student'=>[ { 'name'=>'mikky', 'number'=>'12', 'semester'=>{ 'Number'=>'1', 'subjects'=>'7', 'rank'=>'2' } }, { 'name'=>'vijju', 'number'=>'15', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'10' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'1' } ] }, { 'name'=>'shyam, 'number'=>'15', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'2' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'2' } ] }, ] } } };

but I need to print only data node elements only not university node elements.please halp me.


In reply to help me in perl script to create hash and print some data. by veerubiji

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found