XML::Simple Example perl help needed

D

David Staschover

Hi,

Can someone help me get this working?

Sample XML File test.xml:

<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>


This program gets and prints the values for value1 and value2 without a
problem:

#!/usr/bin/perl
use XML::Simple;
$simple = XML::Simple->new();
$struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
for (@{$struct->{top1}->[0]->{field}})
{
print "
$_->{value1}->[0]
$_->{value2}->[0]
";
}

How can I get the same output using this XML file (I added <top>)?

<top>
<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>
<top>

Thanks in advanced!

David
 
R

Richard Morse

David Staschover said:
Hi,

Can someone help me get this working?

Sample XML File test.xml:

<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>


This program gets and prints the values for value1 and value2 without a
problem:

#!/usr/bin/perl
use XML::Simple;
$simple = XML::Simple->new();
$struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
for (@{$struct->{top1}->[0]->{field}})
{
print "
$_->{value1}->[0]
$_->{value2}->[0]
";
}

How can I get the same output using this XML file (I added <top>)?

<top>
<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>
<top>

Off the top of my head, probably by changing the for loop to be:

for (@{$struct->{top}->{top1}->[0]->{field}}) {

HTH,
Ricky
 
D

David Staschover

That would have made sense, but it didn't work

Richard Morse said:
David Staschover said:
Hi,

Can someone help me get this working?

Sample XML File test.xml:

<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>


This program gets and prints the values for value1 and value2 without a
problem:

#!/usr/bin/perl
use XML::Simple;
$simple = XML::Simple->new();
$struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
for (@{$struct->{top1}->[0]->{field}})
{
print "
$_->{value1}->[0]
$_->{value2}->[0]
";
}

How can I get the same output using this XML file (I added <top>)?

<top>
<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>
<top>

Off the top of my head, probably by changing the for loop to be:

for (@{$struct->{top}->{top1}->[0]->{field}}) {

HTH,
Ricky
 
K

Kevin Collins

David Staschover said:
That would have made sense, but it didn't work

Richard Morse said:
David Staschover said:
Hi,

Can someone help me get this working?

Sample XML File test.xml:

<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>


This program gets and prints the values for value1 and value2 without a
problem:

#!/usr/bin/perl
use XML::Simple;
$simple = XML::Simple->new();
$struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
for (@{$struct->{top1}->[0]->{field}})
{
print "
$_->{value1}->[0]
$_->{value2}->[0]
";
}

How can I get the same output using this XML file (I added <top>)?

<top>
<top1>
<field>
<value1>100</value1>
<value2>test value1</value2>
</field>
<field>
<value1>107</value1>
<value2>test value2</value2>
</field>
</top1>
<top>

Off the top of my head, probably by changing the for loop to be:

for (@{$struct->{top}->{top1}->[0]->{field}}) {

This seems to do the trick:

for (@{$struct->{top}->[0]->{top1}->[0]->{field}})

However, this is only going to give the values for the 1st <top1> ({top1}->[0]}
inside the 1st <top>({top}->[0]), which may not be what you want.

Kevin
 
B

Ben Morrow

[AARGH. Quoting re-arranged. David, please learn not to top-post]


[ snip sample XML file and most of the program ]
$simple = XML::Simple->new();
$struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
for (@{$struct->{top1}->[0]->{field}})

[ more snippage: an element said:
Off the top of my head, probably by changing the for loop to be:

for (@{$struct->{top}->{top1}->[0]->{field}}) {

That would have made sense, but it didn't work

This seems to do the trick:

for (@{$struct->{top}->[0]->{top1}->[0]->{field}})

You don't need all those ->s, and a little whitespace wouldn't hurt.

for (@{ $struct->{top}[0]{top1}[0]{field} }) {

The reason you need the extra [0] is almost certainly because of the
'forcearray => 1' you specified. Do you read the docs for the modules
you use?

Ben
 
D

David Staschover

Not sure how not to top post.. Deleting the text below..

Although I rtfm'd, I have no idea what they're talking about :) The example
below worked.. thanks!
for (@{ $struct->{top}[0]{top1}[0]{field} }) {

The reason you need the extra [0] is almost certainly because of the
'forcearray => 1' you specified. Do you read the docs for the modules
you use?

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.
(e-mail address removed)
 
J

Jim Cochrane

Not sure how not to top post.. Deleting the text below..

Although I rtfm'd, I have no idea what they're talking about :) The example
below worked.. thanks!

Not top posting means to respond to a paragraph or phrase below that
paragraph, as in this example. Top posting means to respond at the
beginning of your post, above the text you are responding to (as you
did), making it harder for people to figure out the context to which
you are responding.

The further down the page the text you are responding to is, the more out
of context your top post is and the harder it is for people to figure
out what you're talking about. Remember, a human mind, though amazing
and powerful, is not a computer - some simple processing tasks are very
taxing for it (unless, perhaps one is autistic).
for (@{ $struct->{top}[0]{top1}[0]{field} }) {

The reason you need the extra [0] is almost certainly because of the
'forcearray => 1' you specified. Do you read the docs for the modules
you use?

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top