perlquestion
khrome
<p> Brief synopsis: I'm pushing a bunch of unstructured data into an API call that returns an XML page. Here's a snipet: </p>
<code>
<?xml version="1.0" encoding="UTF-8"?>
<results>
<url>--removed--</url>
<language>english</language>
<text>---removed---</text>
<taxonomy>
<category>
<label>/vehicle brands/jeep</label>
</category>
<category>
<label>/travel</label>
</category>
</taxonomy>
<keywords>
<keyword>
<text>rear extended bumpstops</text>
</keyword>
</keywords>
</results>
</code>
<p>
So far, I've been using XML::Simple to strip the header, and I've been trying to parse the data as such: </p>
<code>
my $xml = new XML::Simple (KeyAttr=>[]);
my $TopList = $xml->XMLin($result);
$SQL = "INSERT INTO categories (category, url) VALUES(?, ?)";
$SQLX = $dbh->prepare($SQL);
if ($TopList->{taxonomy})
{
foreach my $cat (@{$TopList->{taxonomy}->{category}})
{
$SQLX->execute($cat->{label}, $db_url);
}
$SQLX->finish();
}
</code>
<p> The difficulty is that the XML output is unpredictable. I may not have ANY <category> entries, based on the data. And sometimes, the output comes at me with a 2-word 'keyword' formation, or a hyphenated value. So essentially, I have TWO problems / questions:</p>
<p>1) I need to make a valid check to see if there is actually an entry for the subheading I'm looking for, such as the line above: </p>
<code>if ($TopList->{taxonomy})</code>
<p>This line has never barfed at me, but the next line has, so I need to know if I'm going this check correctly. Reading part 2 might give you a bit more context for this part of my question...</p>
<p>2) The next line in the code barfs at me quite a bit, where I dereference to drill down: </p>
<code>foreach my $cat (@{$TopList->{taxonomy}->{category}}) </code>
<p>Sometimes it barks that it's not a Hash, so I change it, and then it barks that it's not an array. During a chat earlier, I was told this is a common problem with XML::Simple. It was suggested that I use XML::Twigs, or ForceArray. I originally was asking if I could simply use an else clause with my foreach statement. Something like: </p>
<code>foreach $var (@{$TopList->{taxonomy}->{category}})
{do something;}
else foreach $var ($TopList->[taxonomy}->{category})
{do the something this way;} </code>
<p>The more I think about that else clause, the less it makes sense, but the more I think it would be a quick fix to my problem (thus, further cementing the idea that it won't work). Either way, I haven't gotten the syntax to work yet, so I thought I would ask: </p>
<p>How would the Perl Monks do this? </p>
<p>I should also add, that I'm on a very tight deadline, so quick and simple is better than complicated but elegant.</p>
<p>Help me, Perl Monks, You're My Only Hope!</p>
-Khrome