Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Getting the XML node containing a search text...

by biswanath_c (Beadle)
on Oct 26, 2009 at 22:14 UTC ( [id://803346]=perlquestion: print w/replies, xml ) Need Help??

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


Hi

I am trying to write a perl script that'd give me the list of nodes in an XML doc that contain a search string. What's the best way to achieve this goal? I'd prefer getting the XPaths of the nodes that contain the search string.


  • Comment on Getting the XML node containing a search text...

Replies are listed 'Best First'.
Re: Getting the XML node containing a search text...
by toolic (Bishop) on Oct 26, 2009 at 22:43 UTC
    give me the list of nodes in an XML doc that contain a search string
    Can you be more specific regarding what you want to search? Are you trying to match only element names? Element text? Attribute names? Attribute values? All of the above?

    You should post a tiny sample of your XML and the search string.

    XML::Twig has xpath capabilities.


      given a string, I would want to search for all nodes in a an XML document that 'contain' the search string 'in' its value (not exact match). So, i want to search for nodes whose 'value' contain a given search string and would like to get the xpath of all such matching nodes.
Re: Getting the XML node containing a search text...
by ramrod (Curate) on Oct 27, 2009 at 14:47 UTC
    Using XML::LibXML this works for me
    use strict; use XML::LibXML; #Parse XML my $template = 'xmlsample.xml'; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($template); #Search all text nodes for my $node ($doc->findnodes( '//text()' )) #Print xpaths if criteria matched {my $search = $node->nodeValue; if ($search =~ m/InsertSearchCriteriaHere/) {print $node->nodePath();} }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-16 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found