tags:

views:

95

answers:

2

Hi,

I am facing a scenario like this,

I have a XML file, i need to parse it using XML::Simple module (using perl). The output is in Hashes (i use dumper).

I need to convert the output (which is in hash ) to a list or an array. Help me please.

XML:

<employee>
    <name>John Doe</name>
    <age>43</age>
    <sex>M</sex>
    <department>Operations</department>
</employee>

and its output with XML::Simple,

$VAR1 = {
          'department' => 'Operations',
          'name' => 'John Doe',
          'sex' => 'M',
          'age' => '43'
        };
A: 

If you are too lazy to do it by hand, use Hash::Flatten.

rmk
he says he wants to convert it to an array. I don't see that this module does that.
xenoterracide
hey, thanks .. I managed to do this for a simple xml file. but if the xml file has some 2 children, then it is not working.. Parser O/P is given below . Pls help . $VAR1 = { 'emp2' => { 'department' => 'Manitenance', 'name' => 'Doe', 'sex' => 'M', 'age' => '49' }, 'emp1' => { 'department' => 'Operations', 'name' => 'John Doe', 'sex' => 'M', 'age' => '43' } };
Senthil kumar
Ok, now, using the Flatten module will help you get a simpler hash (from more complex xml docs). Next steps: Do the following on your Linux/Unix system:`perldoc -f keys`, and `perldoc -f values`.That should give you some ideas as to how you can begin...
rmk
@Senthil: edit your original question; it will be much easier to read data and code that way.
Ether
A: 

to convert into an array ref

$array_ref = [ %$hash_ref ];

to dereference into an array

@array = @{$array_ref};
xenoterracide
Or maybe s/he means `@$hashref{qw/name age sex department/}`; you don't know what the information given so far
ysth
hey, thanks .. I managed to do this for a simple xml file. but if the xml file has some 2 children, then it is not working.. Parser O/P is given below . Pls help .$VAR1 = { 'emp2' => { 'department' => 'Manitenance', 'name' => 'Doe', 'sex' => 'M', 'age' => '49' }, 'emp1' => { 'department' => 'Operations', 'name' => 'John Doe', 'sex' => 'M', 'age' => '43' } };
Senthil kumar
please learn how to use StackOverflow. I don't really feel like reading that like that. Update your Question
xenoterracide
@senthil: edit your original question; it will be much easier to read data and code that way.
Ether
who down modded me and why?
xenoterracide