For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > XML::Simple Example perl help needed









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 Example perl help needed
David Staschover

2004-05-17, 1:38 pm

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


Richard Morse

2004-05-17, 3:32 pm

In article <dW5qc.244$ri.29178@dfw-read.news.verio.net>,
"David Staschover" <davezx1@yahoo.com> wrote:

> 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

--
Pukku
David Staschover

2004-05-17, 7:31 pm

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

"Richard Morse" <remorse@partners.org> wrote in message
news:remorse-AC8BA7.14031717052004@plato.harvard.edu...
> In article <dW5qc.244$ri.29178@dfw-read.news.verio.net>,
> "David Staschover" <davezx1@yahoo.com> wrote:
>
>
> Off the top of my head, probably by changing the for loop to be:
>
> for (@{$struct->{top}->{top1}->[0]->{field}}) {
>
> HTH,
> Ricky
>
> --
> Pukku



Kevin Collins

2004-05-17, 8:31 pm

In article <TUaqc.254$ri.30053@dfw-read.news.verio.net>, David Staschover
wrote:[color=darkred]
> That would have made sense, but it didn't work
>
> "Richard Morse" <remorse@partners.org> wrote in message
> news:remorse-AC8BA7.14031717052004@plato.harvard.edu...

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

Ben Morrow

2004-05-17, 9:31 pm

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

Quoth Kevin Collins <spamtotrash@toomuchfiction.com>:
> In article <TUaqc.254$ri.30053@dfw-read.news.verio.net>, David Staschover
> wrote:

[ snip sample XML file and most of the program ]
[color=darkred]

[ more snippage: an element <top> has been added as the parent of <top1> ]
[color=darkred]
>
> 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

--
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. ben@morrow.me.uk
David Staschover

2004-05-18, 6:31 pm

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.

ben@morrow.me.uk


Jim Cochrane

2004-05-18, 7:31 pm

In article <xCvqc.288$ri.35893@dfw-read.news.verio.net>, David Staschover wrote:
> 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).

> rain.
> ben@morrow.me.uk
>
>



--
Jim Cochrane; jtc@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
Sponsored Links







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

Copyright 2008 codecomments.com