Problem #1: You ask for the foo nodes that are children of the document. There are no such nodes.
should be$doc->findnodes('foo')
or$doc->findnodes('seg/foo')
$doc->findnodes('/seg/foo')
Problem #2: You ask for the text children of the foo element, but it doesn't have any. It's not even text you want!
should be$foo->findnodes('text()')
which can be simplified tojoin('', map { $_->toString() } $foo_node->childNodes)
join('', $foo_node->childNodes)
Also,
$foo->findvalue('@mid')
is better written as
$foo->getAttribute('mid')
So, we get
use strict; use warnings qw( all ); use feature qw( say ); use XML::LibXML qw( ); my $xml = <<'__EOS__'; <seg><foo mid="0" mtype="seg"><g id="1">Need to export this text</g></ +foo></seg> __EOS__ my $doc = XML::LibXML->new->parse_string($xml); for my $foo_node ($doc->findnodes('/seg/foo')) { my $mid = $foo_node->getAttribute('mid'); my $inner_xml = join('', $foo_node->childNodes); say "$mid $inner_xml"; }
In reply to Re: get text from node - XML::LibXML
by ikegami
in thread get text from node - XML::LibXML
by corfuitl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |