Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Converting XML to array of array.

by ravi179 (Novice)
on Jan 23, 2017 at 09:36 UTC ( #1180142=perlquestion: print w/replies, xml ) Need Help??

ravi179 has asked for the wisdom of the Perl Monks concerning the following question:

How can i convert XML to an array of array.

<?xml version="1.0" encoding="UTF-8"?> <books> <!-- Programming Perl 3ed --> <book id="1"> <title>Programming Perl</title> <edition>3</edition> <authors> <author> <firstname>Larry</firstname> <lastname>Wall</lastname> </author> </authors> <isbn>0-596-00027-8</isbn> </book> <!-- Perl & LWP --> <book id="2"> <title>Perl &amp; </title> <edition >1</edition> <authors> <author> <firstname>Sean</firstname> <lastname>Burke</lastname> </author> </authors> <isbn>0-596-00178-9</isbn> </book> <book id="3"> <!-- Anonymous Perl --> <title>Anonymous Perl</title> <edition>1</edition> <authors /> <isbn>0-555-00178-0</isbn> </book> </books>

I can use only XML::Simple,XML::Parser only,while using XML::Simple I was unable to print the data

my $xl=XMLin('C:/perlpractice/dec8.xml',ForceArray=>1); for$key(keys %$xl) { for $s(keys %{$xl{$key}}) { print $s; } }

Can anyone please help me how to access hashes of hashes with array reference


Replies are listed 'Best First'.
Re: Converting XML to array of array.
by choroba (Archbishop) on Jan 23, 2017 at 10:51 UTC
    Use Data::Dumper to visualise the structure you want to iterate over. You need to supply the root element name at the top: book . The following should give you a clue:

    for my $id (keys %{ $xml->{book} }) { for my $s (keys %{ $xml->{book}{$id} }) { print "$s: @{ $xml->{book}{$id}{$s} }\n"; } }

    Also, don't use XML::Simple.

    Here's how to process the XML in XML::XSH2:

    open file.xml ; for //book echo (title) edition (edition) by (authors/author/firstname | authors/author/lastname) '(' isbn (isbn) ')' ;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Homework exercise probably specifies using XML::Simple or XML::Parser. It may prove useful to know about better alternatives, just not at the moment.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

Re: Converting XML to array of array.
by Corion (Pope) on Jan 23, 2017 at 09:39 UTC
Re: Converting XML to array of array.
by Laurent_R (Canon) on Jan 23, 2017 at 22:47 UTC
    If you looked at the documentation onXML::Simple, you probably saw that this module is deprecated:
    You really don't want to use this module in new code.
    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.
    The major problems with this module are the large number of options (some of which have unfortunate defaults) and the arbitrary ways in which these options interact - often producing unexpected results.
    If you search this forum, you'll find a number of posts explaining why you should not use this module (at least not for new code).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1180142]
Approved by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2020-10-31 21:51 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (291 votes). Check out past polls.