Sunday, 17 August 2014

How can I use Perl to convert an XML file to a differently formatted XML file?



Many radio stations are using a News program that exports stories and newscasts as an XML file:



<?xml version="1.0" encoding="UTF-8"?>
<PublishStory>
<Name>Story Title</Name>
<Description>Description of the Story</Description>
<Story>
<Text>Story Text</Text>
<Media>
<Name>Media Name</Name>
<Description>Description of Media</Description>
<OutCue>Last Sound on Media</OutCue>
<Length>001:11.653</Length>
<FileName>MediaName.wav</FileName>
</Media>
</Story>
<PublishTime>2014-08-18T20:17:41.0000000</PublishTime>
<ExpiresAfter>2014-08-22T20:17:41.0000000</ExpiresAfter>
<RtfFile>Name Of Associated RTF file.rtf</RtfFile>
</PublishStory>


but the format of the XML file is not designed for using RPCXML to publish the story to a website such as Joomla. I have a simple parsing solution:



#!/usr/bin/env perl
use strict;
use warnings;

# use module
use XML::Simple;
use Data::Dumper;

# create object
my $xml = new XML::Simple;

# read XML file
my $data = $xml->XMLin("StartingXMLfile.xml");

# access XML data
print $data->{Name};
print "\n";
print $data->{Description};
print "\n";
print $data->{Story}{Text};
print "\n";
print $data->{Story}{Media}{Description};
print "\n";
print $data->{Story}{Media}{FileName};
print "\n";
print $data->{Story}{Media}{Name};
print "\n";
print $data->{Story}{Media}{Length};
print "\n";
print $data->{PublishTime};
print "\n";
print $data->{ExpiresAfter};
print "\n";


but I'm having trouble coming up with a way to create the XML file for the RPC part. The final XML file would look something like this:



<?xml version="1.0" encoding="UTF-8" ?>
<methodCall>
<methodName><![CDATA[metaWeblog.newPost]]>
</methodName>
<params>
<param><value><string><![CDATA[0]]></string></value></param>
<param><value><string><![CDATA[AccountName]]></string></value></param>
<param><value><string><![CDATA[AccountPsswd]]></string></value></param>
<param><value><struct><member><name><![CDATA[title]]></name>
<value><string><![CDATA[Story Name]]></string></value></member>
<member><name><![CDATA[description]]></name>
<value><string><![CDATA[Story Description]]></string></value></member>
<member><name><![CDATA[categories]]></name>
<value><array><data><value><string><![CDATA[Category]]></string></value></data></array></value></member>
<member><name><![CDATA[mt_keywords]]></name>
<value><string><![CDATA[]]></string></value></member>
<member><name><![CDATA[custom_fields]]></name>
<value><array><data></data></array></value></member>
<member><name><![CDATA[mt_excerpt]]></name>
<value><string><![CDATA[]]></string></value></member></struct></value></param>
<param><value><boolean>1</boolean></value></param>
</params>
</methodCall>


In the end I hope to have something that will run on a cron job that will check the directory for an XML file, convert the file and use curl to post the XML to the website(s). Then remove the original XML and the converted XML.


Thank you.


No comments:

Post a Comment