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

I am using XML::Twig and below is my current twig object. How can I extract just that first text element (the process name)? $twig->text gets me all the text including "176" and "1180".
<process mode="started"> C:\WINDOWS\system32\svchost.exe <pid> 176 </pid> <ppid> 1180 </ppid> </process>

Replies are listed 'Best First'.
Re: XML::Twig text issue
by mirod (Canon) on Jun 20, 2009 at 04:34 UTC

    There are 2 ways to do this: you can take the text of the first text element of the root: $twig->root->first_child( "#TEXT")->text, or you can simply use the text_only method: $twig->root->text_only.

    In both case you might need to trim the result from the extra spaces you get, which in the first option can be done simply by writing $twig->root->first_child( "#TEXT")->trimmed_text

Re: XML::Twig text issue
by toolic (Bishop) on Jun 20, 2009 at 01:54 UTC
    Just a quick suggestion: you could try to first cut_children from the twig object, then get the text.