Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Updated: How to parse my XML file in the following way using XML::Twig

by Ppeoc (Beadle)
on Nov 26, 2015 at 14:04 UTC ( [id://1148667]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks!

I have an XML file to be parsed using XML::Twig. I am getting the following error when I use twig_roots. "Odd number of elements in anonymous hash at line 29. The code partially does what I need without the root handler line. This is what I have done so far. There are many things wrong with my code. The XML code is just a snippet of another similar XML file. The actual XML file I have to parse is very big and would be impossible to call out the children of 'Misc' by their names. 1) Does not work with twig_roots. I am basically looking to display only the elements under Misc.The twig root element is input by the user. 2) With Twig_roots, all my data is printed in one line instead of 6 ( 2 for each Actor). Or even 3 with each actors info in one line 3) I am not sure how to display my header elements just once. I am just printing it in two different files now. Code and XML below!

use strict; use warnings; use XML::Twig; my $start = time; my $flag = 0; open my $fout1, '>', "XMLTest1.csv"; open my $fout2, '>', "XMLTest2.csv"; my $twig = XML::Twig - > new( twig_roots => { 'Misc' => 1, 'Profession' =>1,'Actor' =>1,'Age' =>1 }, twig_handlers => { 'Database' => sub { my($twig, $el) = @_; $twig - > purge; }, 'Database//*' => sub { print $fout2 $_ - > tag, ",", unless(($_ - > has_children( +'#ELT')) || ($flag = 1)); print $fout1 $_ - > text, ",", unless($_ - > has_children( +'#ELT')); $flag = 1; } } ); $twig - > parsefile('Actors.xml'); my $duration = time - $start; print "Execution time: $duration s\n";

My XML file is shown here. I am basically trying to display my data as

|Profession|Actor |Age|Music|TV|Music|TV| |Actor |Ben Affleck|43 |none|Pearl Harbor|none|2001| |Actor |Ben Affleck|43 |Jenny from the block|Wood Will Hunting|200 +2|1997| . . . . |Actor |Leonardo DiCaprio|41|None|Saturday Night Live|None|2014|
<?xml version="1.0" encoding="UTF-8"?> -<Database> <Profession>Actors</Profession> -<Work> -<elt> <Actor>Ben Affleck</Actor> <Age>43</Age> <Place>USA</Place> <Junk/> -<Personal> <Spouse>Jennifer Garner</Spouse> <Kids>Seraphina Rose</Kids> </Personal> -<Movies> -<Comedy> <Movie1>Chasing Amy</Movie1> <Movie2>Gigli</Movie2> <Movie3>Dogma</Movie3> </Comedy> -<Fantasy> <Movie1>Field of dreams</Movie1> <Movie2>None</Movie2> </Fantasy> -<Romance> <Movie1>Shakespeare in Love</Movie1> <Movie2>None</Movie2> </Romance> -<Misc> -<elt> <Music>none</Music> <TV>Pearl Harbor</TV> -<Year> <Music>none</Music> <TV>2001</TV> </Year> </elt> -<elt> <Music>Jenny from the block</Music> <TV>Good Will Hunting</TV> -<Year> <Music>2002</Music> <TV>1997</TV> </Year> </elt> </Misc> </Movies> </elt> -<elt> <Actor>Johnny Depp</Actor> <Age>52</Age> <Place>USA</Place> <Junk/> -<Personal> <Spouse>Amber Heard</Spouse> <Kids>Lilly Rose</Kids> </Personal> -<Movies> -<Comedy> <Movie1>Mortdecai</Movie1> <Movie2>Rango</Movie2> <Movie3>What's eating gilbert grape</Movie3> </Comedy> -<Fantasy> <Movie1>Edward Scissorhands</Movie1> <Movie2>Alice in Wonderland</Movie2> </Fantasy> -<Romance> <Movie1>Chocolat</Movie1> <Movie2>None</Movie2> </Romance> -<Misc> -<elt> <Music>The Snake</Music> <TV>The Run Diary</TV> -<Year> <Music>1994</Music> <TV>2011</TV> </Year> </elt> -<elt> <Music>Be Here Now</Music> <TV>21 Jump Street</TV> -<Year> <Music>1997</Music> <TV>1987</TV> </Year> </elt> </Misc> </Movies> </elt> -<elt> <Actor>Leonardo DiCaprio</Actor> <Age>41</Age> <Place>USA</Place> <Junk/> -<Personal> <Spouse>None</Spouse> <Kids>None</Kids> </Personal> -<Movies> -<Comedy> <Movie1>The Wolf of Wall Street</Movie1> <Movie2>What's Eating Gilbert Grape</Movie2> <Movie3>Don's Plum</Movie3> </Comedy> -<Fantasy> <Movie1>Inception</Movie1> <Movie2>None</Movie2> </Fantasy> -<Romance> <Movie1>Titanic</Movie1> <Movie2>Romeo+Juliet</Movie2> </Romance> -<Misc> -<elt> <Music>None</Music> <TV>Shutter Island</TV> -<Year> <Music>None</Music> <TV>2011</TV> </Year> </elt> -<elt> <Music>None</Music> <TV>Saturday Night Live</TV> -<Year> <Music>None</Music> <TV>2014</TV> </Year> </elt> </Misc> </Movies> </elt> </Work> </Database>

Would appreciate any help with this. Thank you Monks!

Replies are listed 'Best First'.
Re: Error when using twig_roots in XML::Twig
by hippo (Bishop) on Nov 26, 2015 at 14:25 UTC
    Odd number of elements in anonymous hash at line 29
    twig_roots => { 'Misc' },

    There is your anonymous hash with an odd number of elements. It has only one element which is the scalar string 'Misc'. If it is supposed to be a hash it should have key-value pairs instead.

      Thank you for pointing that out. Very stupid of me! A simple twig_roots => { 'Misc' => 1} fixed it Updated my question. Thanks again :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1148667]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found