Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: parsing multi level XML with XML::Simple

by tobyink (Abbot)
on Jan 26, 2012 at 13:32 UTC ( #950087=note: print w/replies, xml ) Need Help??

in reply to parsing multi level XML with XML::Simple

In my experience, XML::Simple usually causes more trouble than it's worth. Its default behaviour is too smart for its own good. I used to always use XML::Simple, but now I've seen the light and haven't used it for years.

use XML::LibXML; my $dom = XML::LibXML->load_xml(IO => \*DATA); foreach my $level ($dom->findnodes('//level | //levels')) { printf("==== level '%s' ====\n", $level->getAttribute('name')); foreach my $child ($level->getChildrenByTagName('*')) { printf("contains %s '%s'\n", $child->nodeName, $child->getAttr +ibute('name')); } print "\n"; } __DATA__ <config> <levels name="a" target="29.6"> <level name="b" weight="50" target="35.1"> <study id="32" name="i" weight="30" /> <study id="36" name="j" weight="70" /> </level> <study id="37" name="k" weight="15" /> <level name="c" weight="13" target="22.1"> <level name="d" weight="69.2"> <level name="e" weight="44.4"> <study id="34" name="l" weight="50" /> <study id="27" name="m" weight="50" /> </level> <level name="f" weight="55.6"> <study id="25" name="n" weight="60" /> <study id="38" name="o" weight="40" /> </level> </level> <level name="g" weight="30.8"> <study id="50" name="p" weight="75" /> <study id="70" name="q" weight="25" /> </level> </level> <level name="h" weight="22" target="19.0"> <study id="40" name="r" weight="90.9" /> <study id="22" name="s" weight="9.1" /> </level> </levels> <study id="19" name="t" weight="0" spec="p0f7=2" /> <study id="19" name="u" weight="0" spec="p0f7=1" /> <study id="62" name="v" weight="0" /> </config>

Replies are listed 'Best First'.
Re^2: parsing multi level XML with XML::Simple
by choroba (Chancellor) on Jan 26, 2012 at 13:46 UTC
    Yes. And once you get tired of typing arrows and long method names, you can switch to XML::XSH2:
    open file.xml ; for (//level | //levels) { echo ==== level @name ==== ; for * echo contains name() @name ; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950087]
[zentara]: a thread goes into a wait state after emitting a wait to all other threads. It locks up a machine completely.
[marto]: if you mean the recent hyperthreading bug, I don't think that's NSA related?
[zentara]: s/thread/cpu/
[marto]: xkcd://538
[Discipulus]: these opcodes mentioned in the wiki pages are the same opcode perl is translated into? or is just the same term but in different fields?
[zentara]: it's useful if you want to lockup a multicore machine :-)
[zentara]: marto I would give up the key easy with drugs....
[Corion]: Discipulus: No, the Perl opcodes are more high-level than the raw processor opcodes
[zentara]: sodium pentathol and a beautiful woman interogator :-)
[Discipulus]: thanks Corion if so i probably never meet them in my life.. ;=)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-07-28 12:12 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (428 votes). Check out past polls.