http://qs321.pair.com?node_id=1097061

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

Brief synopsis: I'm pushing a bunch of unstructured data into an API call that returns an XML page. Here's a snipet:

<?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>

So far, I've been using XML::Simple to strip the header, and I've been trying to parse the data as such:

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(); }

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:

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:

if ($TopList->{taxonomy})

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...

2) The next line in the code barfs at me quite a bit, where I dereference to drill down:

foreach my $cat (@{$TopList->{taxonomy}->{category}})

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:

foreach $var (@{$TopList->{taxonomy}->{category}}) {do something;} else foreach $var ($TopList->[taxonomy}->{category}) {do the something this way;}

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:

How would the Perl Monks do this?

I should also add, that I'm on a very tight deadline, so quick and simple is better than complicated but elegant.

Help me, Perl Monks, You're My Only Hope!

-Khrome