Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Mixed XML content with XML::LibXML

by ikegami (Patriarch)
on Sep 29, 2008 at 13:53 UTC ( [id://714355]=note: print w/replies, xml ) Need Help??


in reply to Mixed XML content with XML::LibXML

Change your query to /root/node/text()

XPath spec

Replies are listed 'Best First'.
Re^2: Mixed XML content with XML::LibXML
by ikegami (Patriarch) on Sep 29, 2008 at 13:58 UTC
    Then again, I think that would return four nodes for
    <root> <node> text1a <child1>data</child1> <child2>data2</child2> text1b </node> <node> text2a <child1>blah</child1> text2b </node> </root>

    If that's a problem, find /root/node elements, then for each of those elements, find child::text().

    Sorry, I don't have XML::LibXML here to test.

      Thanks guys , managed to get it working now.
      use XML::LibXML; my $file = $ARGV[0]; print $file."\n"; &parse($file); sub parse { my $filename = shift; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("$filename"); foreach my $node ($doc->findnodes('/root/node')) { print "textval :".$node->findnodes('./text()')->to_literal."\n"; } }
      This does the trick. The particular data set I'm using should never have more than one text value mixed in with the child nodes.

Log In?
Username:
Password:

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

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

    No recent polls found