Fixing a "bad" XML File
Joe Kline
Purdue.pm
[Generates a GPX file]
[any material that should appear in print but not on the slide]
[Not a valid GPX 1 format]
-
Time tag is in wrong format
-
Mon Feb 06 2012 19:51:42 GMT-0500 (EST)
-
Should be: 2012-02-06T16:50:52Z
[any material that should appear in print but not on the slide]
[Problem 1: I'm paranoid]
-
I could run it through a web site to clean it up.
[any material that should appear in print but not on the slide]
[Problem 2: I'm cheap]
-
I'm guessing the current version is in the proper format
[any material that should appear in print but not on the slide]
[So I wrote some code]
#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;
use XML::Twig;
use DateTime;
use Date::Parse;
[any material that should appear in print but not on the slide]
[So I wrote some code, continued]
#
# change time format from:
# Mon Feb 06 2012 19:51:42 GMT-0500 (EST)
# to 2012-02-06T16:50:52Z
#
my ($file) = @ARGV;
my $twig= XML::Twig->new(
PrettyPrint => 'record',
twig_handlers => { time => \&time_fix }
);
$twig->parsefile($file);
[any material that should appear in print but not on the slide]
[So I wrote some code, continued]
$twig->print();
exit(0);
sub time_fix {
my ($twig, $time_element) = @_;
my $wrong_format = $time_element->text ();
my $time = str2time($wrong_format);
my $date = DateTime->from_epoch( epoch => $time);
$date = $date . q(Z);
$time_element->set_text ($date);
}
[any material that should appear in print but not on the slide]
[The Files]
[any material that should appear in print but not on the slide]