Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: getting the txt of terminal nodes only XML::TWIG

by jkahn (Friar)
on May 01, 2003 at 22:26 UTC ( #254841=note: print w/replies, xml ) Need Help??

in reply to getting the txt of terminal nodes only XML::TWIG

This worked for me, if I understand you correctly:
#!/usr/bin/perl -w use strict; use XML::Twig; my $twig= XML::Twig->new(); my $file = "message.xml"; $twig ->parsefile( $file ); my $root = $twig->root; my @all_text = gather_text($root); print join ("\n---", @all_text), "\n"; sub gather_text { my $node = shift; my (@children) = $node->children(); if (not @children) { # this tag has no children. grab its text data, with no # surrounding tag, and return it. return $node->sprint('NOTAGS'); } else { # recurse into each child my (@text); foreach (@children) { push @text, (gather_text($_)); } return @text; } }
When I test it on the XML of your original post, I get the following results:
[jeremy@serpent pm-test]$ ./ perlquestion --- Isanchez --- Hi, I have to recursively go over xml files (that look very differently from each other) in a folder and collect every content for every tag. I have tried with TWig code but it doesn't work because it grabs all tags including parent tags and then prints first the contents that belong to the parents i.e. all and then the contents again but this time for each doughter node. Can any wise monk give some idea of what to do ? thanks, --- 4 [jeremy@serpent pm-test]$
That's only once for each tag, rather than once for each tag and then again for its children.

Does that help?

(Nice to see you here, Isanchez!)
Update: corrected link to XML.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://254841]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-01-19 09:44 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (216 votes). Check out past polls.