Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Calling nested containers with XML::Simple

by c (Hermit)
on Jul 05, 2002 at 06:09 UTC ( #179571=perlquestion: print w/replies, xml ) Need Help??

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

Okay, last XML::Simple question for a while, I promise.
I am messing around with nested containers and I am not sure how to call a container that doesnt have a "key" assigned.

<global> <snmp> <version>2c</version> <community>public</community> <retries>2</retries> </snmp> <node id=""/> <node id=""/> </global>

With the above xml info, I wanted to do something like

my $i = XMLin("filename",forcearray => 1); print "$i->{snmp}->{version}\n";

This doesn't work, and actually causes the processor to spiral upward until the process is killed. I am sure that I could do something like

<snmp version="2c"> <retries>2</retries> <community>public</community> </snmp>

But I really prefer the nested approach. I'm not sure how to approach this one. Is what I am trying to do feasible?

thanks -c

Replies are listed 'Best First'.
Re: Calling nested containers with XML::Simple
by Kanji (Parson) on Jul 05, 2002 at 06:56 UTC

    You might want to revisist XML::Simple's documentation: forcearray => 1 coerces all nested elements into arrayrefs, turning something like...


    ...into the less aesthetic...

    $i->{snmp}->[0]->{version}->[0] # or $i->{snmp}[0]->{version}[0], which # reads more legibly to me (YMMV)

    You might find a workaround in some of XML::Simple's other options, 'though.


      Thanks! I agree that the second version is not very aesthetic whatsoever. I also found that when the containers are nested, they can be called via


      i had been trying the syntax you show above with the second "->". i think that was the source of my frustration.

      thanks again, your post helped very much. -c

        Whenever i use 'forcearray', i tend to loop:
        for my $snmp (@{$i->{snmp}}) { for my $version (@{$snmp->{version}}) { print "$version\n"; } }
        Now, consider XML::XPath, which 'drills down' to the nodes in question:
        use XML::XPath; my $xp = XML::XPath->new(filename => 'filename'); my $nodeset = $xp->find('global/snmp/version'); print $_->string_value, "\n" for $nodeset->get_nodelist;
        Much nicer. :)


        (the triplet paradiddle with high-hat)
Re: Calling nested containers with XML::Simple
by BrowserUk (Patriarch) on Jul 05, 2002 at 08:30 UTC

    This doesn't work, and actually causes the processor to spiral upward until the process is killed.

    One point not covered yet - the problem you refer to above is not XML::Simple's problem.

    For my own explorations with XML::Simple, and because of warnings that I might need to move to using one of the more complex XML parsers, I wrote a simple testcase generator to create some random, abitrarially complex "well-formed" XML files and subjectivly speaking (I haven't gotten around to using the benchmarking tools yet), XML::Simple proved more than adaquate in terms of both speed and memory usage upto depths of nesting of 20 or more and with upto 20 attributes per node. Thats as far as I bothered to go cos nothing I am working with yet will come even close.

    My point is, that if XML::Simple is running away processing your simple nested example, you have a broken installation I think. I even tried diliberately breaking it by cross-nesting stuff and XML::Simple whirred for a second or two before saying:

    mismatched tag at line ll, column cc, byte bb:

    no matter how deviously I tried to fool it.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2023-02-08 23:23 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (44 votes). Check out past polls.