title: Parsing XML Elements & Attributes with Perl
body: <p>So I wrote some perl that would parse results returned from the <a href="http://aws.amazon.com/" rel="nofollow noreferrer">Amazon Web Services</a>. I am using the <code>XML::Simple</code> package.  For the most part, everything worked when I pulled out an element.  However, the problem I ran into was when an element had an attribute as well.  Then I get an error that the item is a Hash.  </p>
  
  <p>Here's what I did if I wanted to get the Running Time for a DVD: I just created an item to hold the specific info for this one-off item.  </p>
  
  <pre><code>// XML
  &lt;ProductGroup&gt;DVD&lt;/ProductGroup&gt;
  &lt;RunningTime Units="minutes"&gt;90&lt;/RunningTime&gt;
  
  // Perl to parse XML
  
  my $item = $xml-&gt;XMLin($content, KeyAttr =&gt; { Item =&gt; 'ASIN'}, ForceArray =&gt; ['ASIN']);    
  
  $ProductGroup = $item-&gt;{Items}-&gt;{Item}-&gt;{ItemAttributes}-&gt;{ProductGroup};
  
  if(ref($item-&gt;{Items}-&gt;{Item}-&gt;{ItemAttributes}-&gt;{RunningTime}) eq 'HASH'){
      $RunningTimeXML = $xml-&gt;XMLin($content, KeyAttr =&gt; { Item =&gt; 'ASIN'}, NoAttr =&gt; 1);
      $RunningTime = $RunningTimeXML-&gt;{Items}-&gt;{Item}-&gt;{ItemAttributes}-&gt;{RunningTime};
  }
  </code></pre>
  
  <p>Is there a way I can access both elements and attributes from one item?</p>