Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

xpath problem

by avi_2009 (Novice)
on Mar 13, 2009 at 15:51 UTC ( [id://750458]=perlquestion: print w/replies, xml ) Need Help??

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

use strict; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new( xml => 'query.xml' ); print 'There are ', $xp->find('queries/query')->size, " 'query' nodes. +\n"; foreach my $element (qw/topN layer filter name datatype/) { print "$element: ", ( join ', ', map { XML::XPath::XMLParser::as_string($_) =~ />([^<]*)</ } $xp->find("queries/query/$element")->get_nodelist ), "\n"; } ## end foreach my $element (qw/topN layer filter name datatype/) query.xml <?xml version='1.0'?> <queries> <query> <name>topHosts 20</name> <layer>LINK 20</layer> <topN>20</topN> <datatype>topHosts 20</datatype> <filter></filter> </query> <query> <name>topHosts 120</name> <layer>LINK 120</layer> <topN>120</topN> <datatype>topHosts 120</datatype> <filter></filter> </query> </queries>
Hi guys: I was trying this example but getting this error. syntax error at line 1.column 0.byte 0: query.xml ^ at C:/perl/lib/XML/Parser.pm line 187 Please help. Thanks.

Replies are listed 'Best First'.
Re: xpath problem
by Fletch (Bishop) on Mar 13, 2009 at 16:20 UTC

    As a read of the XML::XPath documentation for the new method reveals (documentation explaining how to use things? Balderdash!), the xml argument is for passing literal XML which you want parsed. Your string "query.xml" is not XML, hence you get an error.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: xpath problem
by toolic (Bishop) on Mar 13, 2009 at 16:21 UTC
    This works for me:
    my $xp = XML::XPath->new( filename => 'query.xml' ); ^^^^^^^^
Re: xpath problem
by mirod (Canon) on Mar 13, 2009 at 17:35 UTC

    As often said on this site, do not use XML::XPath, it is not maintained anymore. XML::LibXML is very similar to XML::XPath, except for the fact that it is faster, more powerful, and actively maintained.

    And of course, if you can't use XML::LibXML (because libxml2 is not installed/installable on your system(s)), there is always XML::Rules (hi jenda!) and even XML:Twig.

Re: xpath problem
by ramrod (Curate) on Mar 13, 2009 at 16:21 UTC
    Also, it looks like you're using double quotes and need to add a \ in front of every /.
    For example
    $xp->find("queries/query/$element")->get_nodelist
    should be more like
    $xp->find("queries\/query\/$element")->get_nodelist

Log In?
Username:
Password:

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

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

    No recent polls found