For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > XML::Simple -- can't get anything to print from array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML::Simple -- can't get anything to print from array
Brian Bwarn

2006-07-24, 6:57 pm

I'm just starting out with XML::Simple and can't get
any output to display to STDOUT. What am I missing?

-------------------
Source XML snippet:
-------------------
<dataschemas>
<dataschema name="defaultDB">
<includes>
<include name="Base Metadata"/>
<include name="Extracted Re-Map"/>
</includes>
<attributes>
<attribute category="" parser="TextParser"
extract="true" segmentation="soft"/>
</attributes>
</dataschema>
</dataschemas>

---------------
Perl code:
----------------
# use modules
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple (KeyAttr=>[]);

# read XML file

$data=
$xml->XMLin("C:\datafiles\specialIncludes.xml");

# dereference hash reference
# access <dataschemas> array

print "before loop ...\n";
foreach $d (@{$data->{dataschema}}) {
print "in \$d loop ...\n";
print "dataschema is: ", $d->{includes}->{name},
"\n";
}
print "finished\n";

---------------
Output:
---------------
before loop ...
finished

Thanks, BW

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
DJ Stunks

2006-07-24, 6:57 pm


Brian Bwarn wrote:
> I'm just starting out with XML::Simple and can't get
> any output to display to STDOUT. What am I missing?


you're missing two lines:

use strict;
use warnings;

-jp

Mumia W.

2006-07-24, 9:56 pm

On 07/24/2006 04:59 PM, brian bwarn wrote:
> I'm just starting out with XML::Simple and can't get
> any output to display to STDOUT. What am I missing?
>
> -------------------
> Source XML snippet:
> -------------------
> [snipped]


Use the ForceArray option to make traversal easier. Use
Data::Dumper to look at your data, then de-reference the right
things when you traverse:

# use modules
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple (KeyAttr=>[], ForceArray => 1);

# read XML file

$data=
$xml->XMLin("sp-includes.xml");

# dereference hash reference
# access <dataschemas> array

print "before loop ...\n";

foreach $d (@{$data->{dataschema}}) {
# print Dumper($d);
print "Dataschema: $d->{name}\n";
foreach my $inc (@{$d->{includes}[0]{include}}) {
print " Include: $inc->{name}\n";
}
}

print "finished\n";



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com