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

Im writing a program that takes waypoint positions and converts them into XML using XML::Simple. I am having problems with the scripts.
1. first, the forcearray is forcing all elements to be an array when I just want all but Waypoint to be forced. the XML::Simple docs says to list them and i tried that but it still is not working.
2. In the Waypoint area the code keeps printing => and not =>.
I cant figure out a way to fix these.
The scripts:
#! perl -slw use strict; use IO::Socket; use XML::Simple; $\="\n"; for (;;){ ################################# #Waypoint Functions: print "%%% Waypoint GO %%%"; open TEMP, '<', '/home/uas/Scripts/WP/waytemp' or die "cannot +open 'waytemp' $!"; open FINAL, '<', '/home/uas/Scripts/WP/wayfinal' or die "canno +t open 'wayfinal' $!"; #check for a new waypoint file my @tstat = stat(TEMP); my @fstat = stat(FINAL); #If waytemp is newer send it to VC if ($tstat[9] >= $fstat[9]) { print "### NEW File ###"; my $xml = new XML::Simple ( RootName => 'Task', XMLDecl => + '<?xml version="1.0" encoding="UTF-8"?>', ForceArray => [ 'ID' ]); my @temp = <TEMP>; my @final = <FINAL>; close TEMP; close FINAL; my @waypoints; my $count = 0; foreach (@temp) { my @point = split(',', $_); push(@waypoints, "{'Longitude' => '$point[0]', 'Latitu +de' => '$point[1]', 'SequenceNumber' => '$count'}"); $count++; } print "### Create XML ###"; my $waydata= { 'xmlns' => "http://Task.CUIntegration.com", + 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xsi:sch +emaLocation' => "http://Task.CUIntegration.com Task.xsd ", 'ID' => 'ID', 'Type' => ['Loop'], 'Destination' => ['Velocity'], 'Waypoint' => [ "@waypoints"], }; print $waydata; my $textdata = $xml->XMLout($waydata); print $textdata; #print "--- SENDING ---"; #send($way_client, $textdata, 0); print "%%% DONE %%%"; } print "SLEEP"; sleep 10; }

And outputs this:
%%% Waypoint GO %%% ### NEW File ### ### Create XML ### HASH(0x97dbeb8) <?xml version="1.0" encoding="UTF-8"?> <Task ID="ID" xmlns="http://Task.CUIntegration.com" xmlns:xsi="http:// +www.w3.org /2001/XMLSchema-instance" xsi:schemaLocation="http://Task.CUIntegratio +n.com Task .xsd "> <Destination>Velocity</Destination> <Type>Loop</Type> <Waypoint>{'Longitude' =&gt; '40.56', 'Latitude' =&gt; ' -106.25 ', 'SequenceNumber' =&gt; '0'} {'Longitude' =&gt; '38.89', 'Latitude' +=&gt; ' -1 05.44 ', 'SequenceNumber' =&gt; '1'} {'Longitude' =&gt; '40.31', 'Latitude' +=&gt; ' -1 03.82', 'SequenceNumber' =&gt; '2'}</Waypoint> </Task> %%% DONE %%% SLEEP

and needs to look like this:
<?xml version="1.0" encoding="UTF-8"?> <Task xmlns="http://Task.CUIntegration.com" xmlns:xsi="http://www.w3.o +rg/2001/XMLSchema-instance" xsi:schemaLocation="http://Task.CUIntegra +tion.com Task.xsd "> <ID>ID</ID> <Type>Loop</Type> <Destination>Velocity</Destination> <Waypoint Latitude="40.147622" Longitude="-105.240443" SequenceNumber +="0"/> <Waypoint Latitude="40.146132" Longitude="-105.246007" SequenceNumber +="1"/> <Waypoint Latitude="40.139455" Longitude="-105.246007" SequenceNumber +="2"/> <Waypoint Latitude="40.140011" Longitude="-105.253101" SequenceNumber +="3"/> <Waypoint Latitude="40.133751" Longitude="-105.255049" SequenceNumber +="4"/> </Task>

Replies are listed 'Best First'.
Re: XML problems
by pc88mxer (Vicar) on May 06, 2008 at 23:07 UTC
    This is certainly not what you want:
    'Waypoint' => [ "@waypoints"],
    The double quotes will stringify the array which completely removes the array structure. Also, I just noticed that you have a problem in the way your construct @waypoints:
    push(@waypoints, "{'Longitude' => '$point[0]', 'Latitu +de' => '$point[1]', 'SequenceNumber' => '$count'}");
    You don't want double-quotes (or need single quotes) here either:
    push(@waypoints, { Longitude => $point[0], Latitude => $point[1], SequenceNumber => $count } );
    Combined with the not using stringification should get you pretty close to what you want.

    Update: Here's some code that seems to produce what you want:

    use strict; use warnings; use XML::Simple; my $xml = new XML::Simple ( RootName => 'Task', XMLDecl => '<?xml version="1.0" encoding="UTF-8"?>', ); my @waypoints = ( { Long => 1, Lat => 2, SeqNo => 1 }, { Long => 3, Lat => 4, SeqNo => 2 }, ); my $waydata = { 'xmlns' => "http://Task.CUIntegration.com", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xsi:schemaLocation' => "http://Task.CUIntegration.com Task.xsd", 'ID' => ['ID'], 'Type' => ['Loop'], 'Destination' => ['Velocity'], 'Waypoint' => [ @waypoints ], }; print $xml->XMLout($waydata);