I am trying to filter through a large XML file, pull out the elements I need within the value range restricted by the IF statement, and export them to CSV. However, I am a newb to programming and this is my first exposure to Perl, so I get syntax errors on lines 14 and 15 and probably doing other things wrong too. Please help me straighten this out!
Here's my code:
#! /usr/bin/perl -w use strict; use XML::Twig; my $twig= new XML::Twig( TwigHandlers => {town => \&town} ); if( my $file= $ARGV[0]) {$twig->parsefile( 'datafile_towns.xml'); $twig->flush;} sub town { my ($twig, $town) = @_; if (my $townx($town->first_child('location')->first_child('mapx'))>5 +00){ print $town->first_child('towndata')->first_child_text('townname') +, ',', $town->first_child('player')->first_child_text('allianc +eticker'), ',', $town->first_child('player')->first_child_text('playern +ame'), ',', $town->first_child('towndata')->first_child_text('townn +ame'), ',', $town->first_child('location')->first_child_text('mapy' +), ',', $town->first_child_text('location')->first_child_text(' +mapx'), print "\n"; }; $twig->flush; }
And here's a snippet of XML:
<towns> <server> <name>servername</name> <servername>servername</servername> </server> <town> <location> <mapx>383</mapx> <mapy>-1815</mapy> <terraintype id="9">terraintype</terraintype> <terrainoveralltype id="4">terraintype</terrainoveralltype> </location> <player> <playername id="1">somedude</playername> <playerrace>Human</playerrace> <playeralliance> <alliancename id="18">somealliance</alliancename> <allianceticker>AL</allianceticker> <alliancetaxrate>0.01</alliancetaxrate> </playeralliance> </player> <towndata> <townname id="1">sometown</townname> <foundeddatetime>date</foundeddatetime> <population>1</population> <iscapitalcity>1</iscapitalcity> <isalliancecapitalcity>1</isalliancecapitalcity> </towndata> </town>
In reply to TWIGging XML to CSV: what's wrong with my syntax? by tinker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |