Do you know where your variables are? | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Brief synopsis: I'm pushing a bunch of unstructured data into an API call that returns an XML page. Here's a snipet:
So far, I've been using XML::Simple to strip the header, and I've been trying to parse the data as such:
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:
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! -KhromeIn reply to Help with Parsing XML output by khrome
|
|