Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

xml::twig gathering all element and att and its value question

by convenientstore (Pilgrim)
on Nov 10, 2008 at 10:12 UTC ( #722601=perlquestion: print w/replies, xml ) Need Help??

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

one more question for xml::twig

Is there any method from xml::twig which will grab all element and attribute and attribute value?

for example
<config> <computer id="one" type="mac" os="XP" > <lease true="yes" /> <extra_device value="scanner"/> </computer> <computer id="two" type="pc" os="NT" > <lease true="no" /> </computer> </config>
Given above xml file, if I wanted to grab all element and attribute and its value using xml::twig and make a report which should look like


computer report ----------------- computer id: one type: mac os:XP lease:yes extra_device=scanner computer id: two type: pc os:NT lease:no ------------------
What would be the best way to get this done w/ xml::twig?
If someone can give me a pointer for this, would appreciate it very much

Replies are listed 'Best First'.
Re: xml::twig gathering all element and att and its value question
by GrandFather (Saint) on Nov 10, 2008 at 10:22 UTC
Re: xml::twig gathering all element and att and its value question
by mirod (Canon) on Nov 10, 2008 at 14:48 UTC

    I agree with GrandFather here. In that specific case you don't use at all the fact that XML::Twig gives you trees. You are interested in the tags as such. XML::Parser should be enough for that.

      but suppose I am already using xml::twig .. is it common for script to also use xml::parser in one script?

        Reread the links given in my previous reply and ask questions about that code if you need to. Ya ain't never gonna lern ifn ya don try!


        Perl reduces RSI - it saves typing
Re: xml::twig gathering all element and att and its value question
by Jenda (Abbot) on Nov 12, 2008 at 15:15 UTC
    use XML::Rules; XML::Rules->new( stripspaces => 7, rules => { lease => sub {return 'lease' => $_[1]->{true}}, extra_device => sub {return 'extra_device' => $_[1]->{value}}, computer => sub { if ($_[1]->{extra_device}) { print "computer id: $_[1]->{id} type: $_[1]->{type} os +: $_[1]->{os} lease:$_[1]->{lease} extra_device=$_[1]->{extra_device} +\n"; } else { print "computer id: $_[1]->{id} type: $_[1]->{type} os +: $_[1]->{os} lease:$_[1]->{lease}\n"; } return; } } )->parse(\*DATA); __DATA__ <config> <computer id="one" type="mac" os="XP" > <lease true="yes" /> <extra_device value="scanner"/> </computer> <computer id="two" type="pc" os="NT" > <lease true="no" /> </computer> </config>

    Sometimes it's better to reach for a different hammer. Even if you already have a hammer in your hand.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2022-05-19 03:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?