Discussion:
XML::Simple -- can't get anything to print from array
(too old to reply)
Brian Bwarn
2006-07-24 21:59:59 UTC
Permalink
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 22:31:40 UTC
Permalink
Post by Brian Bwarn
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.18.spam+ (Mumia W.)
2006-07-25 00:11:33 UTC
Permalink
Post by Brian Bwarn
I'm just starting out with XML::Simple and can't get
any output to display to STDOUT. What am I missing?
-------------------
-------------------
[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";
Venkat Saranathan
2006-07-25 13:18:10 UTC
Permalink
The data structure is little more complicated than list of hashes due to the
attributes in XML.


Here is an updated version of your perl code.

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

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

# read XML file

$data=
$xml->XMLin("C:\\temp\\input.xml");

# dereference hash reference
# access <dataschemas> array

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

}
print "finished\n";



with warm regards,
Venkat Saranathan
Gulf Breeze Software
www.gulfsoft.com

GulfBreeze Blog
www.gulfsoft.com/blog


-----Original Message-----
From: brian bwarn [mailto:***@yahoo.com]
Sent: Monday, July 24, 2006 6:00 PM
To: ***@perl.org
Subject: XML::Simple -- can't get anything to print from array


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
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Brian Bwarn
2006-07-25 15:49:33 UTC
Permalink
Thank you both for your replies. I'm still not having
success with either Venkat's or Mumia's version of the
foreach loop. I tried an example of something that
should work--namely Listing C using the source in
Listing A of the how-to article at
http://builder.com.com/5100-6371-5363190.html.

I can't get anything to output with that either.

Is there something I should look for in my
configuration / module setup (I'm running perl v.5.6.1
(ActiveState Build 638) using the latest versions of
XML-Simple and Data-Dumper provided at
http://ppm.activestate.com/PPMPackages/zips/6xx-builds-only/.

Thanks again,
BW
Post by Venkat Saranathan
The data structure is little more complicated than
list of hashes due to the
attributes in XML.
Here is an updated version of your perl code.
#use modules
use XML::Simple;
use Data::Dumper;
# create object
$xml = new XML::Simple (KeyAttr=>[]);
# read XML file
$data=
$xml->XMLin("C:\\temp\\input.xml");
# dereference hash reference
# access <dataschemas> array
print "before loop ...\n";
foreach $d
print "in \$d loop ...\n";
print $d->{name}, "\n";
}
print "finished\n";
with warm regards,
Venkat Saranathan
Gulf Breeze Software
www.gulfsoft.com
GulfBreeze Blog
www.gulfsoft.com/blog
-----Original Message-----
Sent: Monday, July 24, 2006 6:00 PM
Subject: XML::Simple -- can't get anything to print
from array
I'm just starting out with XML::Simple and can't get
any output to display to STDOUT. What am I missing?
-------------------
-------------------
<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>
---------------
----------------
# 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";
print "in \$d loop ...\n";
print "dataschema is: ", $d->{includes}->{name},
"\n";
}
print "finished\n";
---------------
---------------
before loop ...
finished
Thanks, BW
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam
protection around
http://mail.yahoo.com
--
<http://learn.perl.org/>
<http://learn.perl.org/first-response>
--
<http://learn.perl.org/>
<http://learn.perl.org/first-response>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
mumia.w.18.spam+ (Mumia W.)
2006-07-25 18:13:18 UTC
Permalink
Post by Brian Bwarn
Thank you both for your replies. I'm still not having
success with either Venkat's or Mumia's version of the
foreach loop. [...]
Did you enable ForceArray?

Did you use Data::Dumper to look at the structure of what
XML::Simple returned?
Bw
2006-07-25 20:36:22 UTC
Permalink
Post by mumia.w.18.spam+ (Mumia W.)
Did you enable ForceArray?
Did you use Data::Dumper to look at the structure of
what XML::Simple returned?

Yes to both. I've attached the Dumper output.
(outp.txt). Given the attached xml file, and your
first snippet/example, I tried to do something very
simple: print out my dataschema name:

foreach $d (@{$data->{dataschemas}}) {
print "Dataschema: $d->{name}\n";
}

It won't print. I won't include all the variants on
the $d->{name} line I've tried, but all the results
are the same: nothing outputs.

BW

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
mumia.w.18.spam+ (Mumia W.)
2006-07-25 21:45:30 UTC
Permalink
Post by Bw
Post by mumia.w.18.spam+ (Mumia W.)
Did you enable ForceArray?
Did you use Data::Dumper to look at the structure of
what XML::Simple returned?
Yes to both. I've attached the Dumper output.
(outp.txt). Given the attached xml file, and your
first snippet/example, I tried to do something very
Why {dataschemas} when the data is in {dataschema} (no "s")?
Post by Bw
print "Dataschema: $d->{name}\n";
}
It won't print. I won't include all the variants on
the $d->{name} line I've tried, but all the results
are the same: nothing outputs.
[...]
$VAR1 = {
'dataschema' => {
[...]
This was the purpose of using Data::Dumper--to show that all
of the data is under {dataschema}. People use Data::Dumper to
find out what the structure of their data is, and then write a
program to deal with that structure.

Also, please enable strictures and warnings for your program
by placing these lines at the top:

use strict;
use warnings;

Then modify your program to run under these conditions (a lot
of work, but it pays off in bug prevention.)

From the XML::Simple doc, did you read this?
Post by Bw
· check out "ForceArray" because you'll almost certainly want to turn
it on
XML::Simple does some pretty weird stuff unless you use
ForceArray, so always use ForceArray; ForceArray is what makes
XML::Simple simple.

And please don't snip attributions.
Bw
2006-07-26 16:08:28 UTC
Permalink
Post by mumia.w.18.spam+ (Mumia W.)
And please don't snip attributions.
Firstly, to make sure I'm exercising proper etiquette
here, could you please explain what I snipped so that
I don't do that in the future?

I finally got this working. I'd been leaving out one
of the items in my hash dereference string. Once I
put that item in place, I started seeing output.

Thanks for all your assistance.
Post by mumia.w.18.spam+ (Mumia W.)
Post by Bw
Post by mumia.w.18.spam+ (Mumia W.)
Did you enable ForceArray?
Did you use Data::Dumper to look at the structure
of
Post by Bw
Post by mumia.w.18.spam+ (Mumia W.)
what XML::Simple returned?
Yes to both. I've attached the Dumper output.
(outp.txt). Given the attached xml file, and your
first snippet/example, I tried to do something
very
Why {dataschemas} when the data is in {dataschema}
(no "s")?
Post by Bw
print "Dataschema: $d->{name}\n";
}
It won't print. I won't include all the variants
on
Post by Bw
the $d->{name} line I've tried, but all the
results
Post by Bw
are the same: nothing outputs.
[...]
$VAR1 = {
'dataschema' => {
[...]
This was the purpose of using Data::Dumper--to show
that all
of the data is under {dataschema}. People use
Data::Dumper to
find out what the structure of their data is, and
then write a
program to deal with that structure.
Also, please enable strictures and warnings for your
program
use strict;
use warnings;
Then modify your program to run under these
conditions (a lot
of work, but it pays off in bug prevention.)
From the XML::Simple doc, did you read this?
Post by Bw
� check out "ForceArray" because you'll
almost
Post by mumia.w.18.spam+ (Mumia W.)
certainly want to turn
Post by Bw
it on
XML::Simple does some pretty weird stuff unless you
use
ForceArray, so always use ForceArray; ForceArray is
what makes
XML::Simple simple.
And please don't snip attributions.
--
<http://learn.perl.org/>
<http://learn.perl.org/first-response>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
mumia.w.18.spam+ (Mumia W.)
2006-07-26 16:22:58 UTC
Permalink
Post by Bw
Post by mumia.w.18.spam+ (Mumia W.)
And please don't snip attributions.
Firstly, to make sure I'm exercising proper etiquette
here, could you please explain what I snipped so that
I don't do that in the future?
I finally got this working. I'd been leaving out one
of the items in my hash dereference string. Once I
put that item in place, I started seeing output.
Thanks for all your assistance.
You're welcome.
That's an attribution.
Bw
2006-07-26 17:33:44 UTC
Permalink
How (assuming it does) does XML::Simple deal with an
XML element whose value changes throughout the course
of the Dumper output?

XML::Simple code snippet:
$data->{dataschemas}->{dataschema}{changing element
name}->{attributes}->{attribute}}

Dumper output snippet:

(format adjusted for brevity):
{
'dataschemas' => {
'dataschema' => {
'changing element name' => {
'attributes' => {
'attribute' => { [
... attributes here
...
}
}
}
}
}
}

If XML::Simple isn't the right module for the task,
which is a better one? I looked at the 'where to from
here?' section of the XML::Simple doc to answer this,
but none of the modules briefly described there seem
to be likely choices at this point.

Thanks,
BW

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Bw
2006-07-26 19:44:10 UTC
Permalink
I'm not sure that I was completely clear on my
description. As I go through the Dumper output, I
have a different name/entry for each occurence of the
{changing element name} element. I'm looking for
recommendations regarding how to handle such
occurrences.

I looked through the docs and have (to see the first
occurrence) tried in the code:

$data->{dataschemas}->{dataschema}->[0]->{attributes}->{attribute}}
and
$data->{dataschemas}->{dataschema}[0]->{attributes}->{attribute}}

Neither worked and I'm not sure from the XML::Simple
documentation what other options I have to identify
the varying element name in the code.
Post by Bw
How (assuming it does) does XML::Simple deal with an
XML element whose value changes throughout the
course
of the Dumper output?
$data->{dataschemas}->{dataschema}{changing element
name}->{attributes}->{attribute}}
{
'dataschemas' => {
'dataschema' => {
'changing element name' => {
'attributes' => {
'attribute' => { [
... attributes here
...
}
}
}
}
}
}
If XML::Simple isn't the right module for the task,
which is a better one? I looked at the 'where to
from
here?' section of the XML::Simple doc to answer
this,
but none of the modules briefly described there seem
to be likely choices at this point.
Thanks,
BW
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam
protection around
http://mail.yahoo.com
--
<http://learn.perl.org/>
<http://learn.perl.org/first-response>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
DJ Stunks
2006-07-26 21:35:05 UTC
Permalink
Post by Bw
I'm not sure that I was completely clear on my
description. As I go through the Dumper output, I
have a different name/entry for each occurence of the
{changing element name} element. I'm looking for
recommendations regarding how to handle such
occurrences.
I looked through the docs and have (to see the first
$data->{dataschemas}->{dataschema}->[0]->{attributes}->{attribute}}
and
$data->{dataschemas}->{dataschema}[0]->{attributes}->{attribute}}
Neither worked and I'm not sure from the XML::Simple
documentation what other options I have to identify
the varying element name in the code.
I'd like to try to help, but I wasn't able to download the .xml and the
.txt you uploaded earlier in this thread.

anyway, it doesn't sound like your problem is with XML::Simple but
learning how to access and manipulate a complex data structure (in this
case returned from XML::Simple). You would do well to read perlreftut
and perldsc in order to understand how to access them.

in your case, the changing key names, will be keys to a hash. if you
don't know what the key names are ahead of time, just use the keys()
function to get a list of them.

HTH,
-jp
mumia.w.18.spam+ (Mumia W.)
2006-07-27 00:03:09 UTC
Permalink
Post by Bw
I'm not sure that I was completely clear on my
description. As I go through the Dumper output, I
have a different name/entry for each occurence of the
{changing element name} element. I'm looking for
recommendations regarding how to handle such
occurrences.
[...]
You handle them the way you'd handle any hash reference. You
iterate over the keys:

use strict;
use warnings;

my $xml = new XML::VeryComplex (KeyAttr=>[]);
my $data = $xml->XMLin(join '',<DATA>);


my $variable = $data->{dataschema}{attributes}{attribute};
print "attribute:\n";
foreach my $key (keys %$variable) {
print qq{ $key => "$variable->{$key}"\n};
}


exit;

package XML::VeryComplex;
use base 'XML::Simple';

package main;
__DATA__
<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>

Venkat Saranathan
2006-07-25 19:55:27 UTC
Permalink
Could be. Because, the code that I sent works fine in my environment.

C:\temp>perl -v

This is perl, v5.8.7 built for MSWin32-x86-multi-thread
(with 14 registered patches, see perl -V for more detail)

Copyright 1987-2005, Larry Wall

Binary build 815 [211909] provided by ActiveState http://www.ActiveState.com
ActiveState is a division of Sophos.
Built Nov 2 2005 08:44:52

Perl may be copied only under the terms of either the Artistic License or
the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


C:\temp>perl xml.pl
before loop ...
in $d loop ...
Base Metadata
in $d loop ...
Extracted Re-Map
finished

C:\temp>



with warm regards,
Venkat Saranathan
Gulf Breeze Software
www.gulfsoft.com

GulfBreeze Blog
www.gulfsoft.com/blog


-----Original Message-----
From: brian bwarn [mailto:***@yahoo.com]
Sent: Tuesday, July 25, 2006 11:50 AM
To: ***@perl.org
Subject: RE: XML: :Simple -- can't print still: config problem?


Thank you both for your replies. I'm still not having
success with either Venkat's or Mumia's version of the
foreach loop. I tried an example of something that
should work--namely Listing C using the source in
Listing A of the how-to article at
http://builder.com.com/5100-6371-5363190.html.

I can't get anything to output with that either.

Is there something I should look for in my
configuration / module setup (I'm running perl v.5.6.1
(ActiveState Build 638) using the latest versions of
XML-Simple and Data-Dumper provided at
http://ppm.activestate.com/PPMPackages/zips/6xx-builds-only/.

Thanks again,
BW
Post by Venkat Saranathan
The data structure is little more complicated than
list of hashes due to the
attributes in XML.
Here is an updated version of your perl code.
#use modules
use XML::Simple;
use Data::Dumper;
# create object
$xml = new XML::Simple (KeyAttr=>[]);
# read XML file
$data=
$xml->XMLin("C:\\temp\\input.xml");
# dereference hash reference
# access <dataschemas> array
print "before loop ...\n";
foreach $d
print "in \$d loop ...\n";
print $d->{name}, "\n";
}
print "finished\n";
with warm regards,
Venkat Saranathan
Gulf Breeze Software
www.gulfsoft.com
GulfBreeze Blog
www.gulfsoft.com/blog
-----Original Message-----
Sent: Monday, July 24, 2006 6:00 PM
Subject: XML::Simple -- can't get anything to print
from array
I'm just starting out with XML::Simple and can't get
any output to display to STDOUT. What am I missing?
-------------------
-------------------
<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>
---------------
----------------
# 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";
print "in \$d loop ...\n";
print "dataschema is: ", $d->{includes}->{name},
"\n";
}
print "finished\n";
---------------
---------------
before loop ...
finished
Thanks, BW
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam
protection around
http://mail.yahoo.com
--
<http://learn.perl.org/>
<http://learn.perl.org/first-response>
--
<http://learn.perl.org/>
<http://learn.perl.org/first-response>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
DJ Stunks
2006-07-25 20:26:43 UTC
Permalink
Post by Venkat Saranathan
Could be. Because, the code that I sent works fine in my environment.
what could be?? please don't top post.

-jp
Loading...