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

Re: Parsing XML file for more than 1 child element with attributes (XML::Twig)

by Tanktalus (Canon)
on Feb 06, 2008 at 15:39 UTC ( [id://666590]=note: print w/replies, xml ) Need Help??


in reply to Parsing XML file for more than 1 child element with attributes

XML::Simple may work great for simple stuff, and though your new requirement may still be simple, it seems like it won't take long for you to surpass that. Personally, I find XML::Twig simple enough for the simple stuff, and no more complex for the complex stuff... so I'd recommend going that way sooner rather than later.

That said, this doesn't appear to be valid XML: there is no close tag for 'report'. So it seems unfortunate that XML::Simple doesn't complain (it's not really XML anymore). Anyway, if I just close that tag immediately, I get:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new(); $twig->parse(\*DATA); for my $node ($twig->get_xpath('/root/node')) { print "Machine: ", $node->att('mid'), "\n"; my ($values) = $node->get_xpath('values'); print "\tvalues: ", $values->text(), "\n"; my ($time) = $node->get_xpath('time'); print "\ttime: ", $time->text(), "\n"; } __END__ <root> <report id="0" ip="1.1.1.1" /> <node mid="machine1" name="Winxp"> <values>1, 2, 3, 4, 5, 6</values> <time>110, 120, 130, 140, 150, 160</time> </node> <node mid="machine2" name="Win2003"> <values>1, 2, 3, 4, 5, 6</values> <time>110, 120, 130, 140, 150, 160</time> </node> </root>
And that gives:
Machine: machine1 values: 1, 2, 3, 4, 5, 6 time: 110, 120, 130, 140, 150, 160 Machine: machine2 values: 1, 2, 3, 4, 5, 6 time: 110, 120, 130, 140, 150, 160
If you close the report after the last node, the $twig->get_xpath will have to change to /root/report/node, or you can just use //node which will work either way.

Replies are listed 'Best First'.
Re^2: Parsing XML file for more than 1 child element with attributes (XML::Twig)
by Cody Pendant (Prior) on Feb 07, 2008 at 03:01 UTC
    Just for the record, XML::Simple does choke on the non-well-formed XML. It dies with a very useful message.

    I fixed the bad XML but didn't mention it for my example.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
      Thanks Cody Pendant and Tanktalus. my $simple = XML::Simple -> new (KeyAttr => 'node'); I thought the above was simple as the xml tree is returned as an hash of an hash and so on. But when i saw the structure entries like and made me think. Since i want a subroutine it will be easy for me to use. Hence i plan to use use XML::Twig; I can pass the XML as a string, mid and name to the subroutine. The return from the subroutine can be the values and timestamps.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found