Greetings Monks.
I have been wrestling with perl in xml for a couple weeks now. I have gotten to the point where I need to ask for help. Here's the situ. I'm sure there are easier solutions, but due to the constraints of the system I'm working in, I cannot download and install additional perl modules, and am stuck trying to get my xml tasks taken care of using XML::Pareser and XML::Simple.
I seem to have no trouble parsing through single xml elements. However I seem to be having trouble when it comes to the multiple elements, which are displayed as ARRAY(0...). I'm obviously missing something here and I'm ready to be schooled.
A simplified sampling of my code follows.
prod.xml:
<shelf>
<product>
<title>widget1</title>
<cost>0.10</cost>
<rating>B</rating>
<color>red</color>
<color>blue</color>
<color>green</color>
</product>
<product>
<title>widget2</title>
<cost>0.25</cost>
<rating>S</rating>
<color>pink</color>
<color>gray</color>
<color>orange</color>
</product>
</shelf>
prod.pl
use XML::Simple;
$file = XMLin('./prod.xml');
foreach $products (@{$file->{product}}) {
print "Widget: " . $products->{title} ." \n";
print "Rating: " . $products->{rating} . "\n";
print " Cost: " . $products->{cost} . "\n";
print "Colors: " . $products->{color} . "\n\n";
}
Output
Widget: widget1
Rating: B
Cost: 0.10
Colors: ARRAY(0x19c5584)
Widget: widget2
Rating: S
Cost: 0.25
Colors: ARRAY(0x19c55f0)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.