Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

XML Simple

by ddragosa (Acolyte)
on Jan 25, 2010 at 13:57 UTC ( [id://819486]=perlquestion: print w/replies, xml ) Need Help??

ddragosa has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to do something like this in a perl program:
use XML::Simple; use Data::Dumper; use XML::Parser; my $simple = XML::Simple->new (ForceArray => 1, KeepRoot => 1); my $data = $simple->XMLin('template_POS.ssp'); $VAR1 = { 'profile' => [ { 'protocol' => 'Hypercom', 'scenario' => [ { 'test' => [ { 'id' => '001', 'channel' => 'Non +e', 'message_data' => + [ + { + 'H001' => [ + { + 'i' => [ + '**MS**' + ], + 'o' => [ + '**MS**' + ] + } + ], etc...
print $data->{profile}[0]->{protocol};
print "\n";
--> this is ok
but this:
print $data->{profile}[0]->{scenario}[0]->{test}[0]->{id};
-->NOK (Not an ARRAY reference at...)
Can anyone help me display the value of {id}? Thank you.

Replies are listed 'Best First'.
Re: XML Simple
by jettero (Monsignor) on Jan 25, 2010 at 14:03 UTC
    XML::SImple has to guess. Sometimes it guesses wrong no matter what you do. Then its arguments get rather un-simple while you try to shoehorn it in. I mean, your code and your example data look fine, so I can only assume the next file didn't come out in the same-ly shaped data structure.

    You're much better off using something like XML::Twig or one of its cousins. Then you can say precisely what you wish to find and how to process it. Personally, I was mad about wrong guesses by Simple and too much work in Twig. So, I wrote XML::CuteQueries as a kind of thought experiment. I'm not exactly recommending using it — in fact it has serious flaws, but I think I kindof like it. It's cute.

    Try Twig.

    -Paul

Re: XML Simple
by Jenda (Abbot) on Jan 25, 2010 at 14:25 UTC

    If your data looks like you show, the statement should work. If it doesn't try to cut off from the end till it does to find the step that causes problems.

    And as jettero suggests, try to have a look at other options that would give you a simpler datastructure or a way to query for the stuff you are interested in. Search PerlMonks for XML::Rules for example.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      I like the look of XML::Rules, but I read the pod, rather than searching here. It looks like a smarter attempt at what I was going for in CQ.

      -Paul

        There's a few examples on PerlMonks and the explanation of the ideas that brought it to life. It seems to get to what you were after from a different angle and as such is probably better in different cases. In case you want just a fairly small subset of the data in the XML and XPath is a good way to select what you want, then CQ will most likely work better :-) If on the other hand you want a bigger part of the data and the resulting structure is more complex then the XML::Rules' style will probably end up being more readable. Plus it allows you to execute code at any level to trim or massage the data structure further.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://819486]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found