How can I use Perl's XML::LibXML to extract content between tags? -
i have xml file content this:
<node id="7"/> www <node id="10"/> how possible using xml::libxml , perl take content between 2 nodes, ie "www"?
thank you.
the xml format have deal awful!*
given node, want nodes siblings, follow (except perhaps intermediary comments) , text nodes.
use strict; use warnings; use feature qw( ); use xml::libxml qw( xml_comment_node xml_text_node ); sub following_text { ($node) = @_; $text = ''; while ($node = $node->nextsibling()) { $node_type = $node->nodetype(); next if $node_type == xml_comment_node; last if $node_type != xml_text_node; $text .= $node->data(); } return $text; } $parser = xml::libxml->new(); $doc = $parser->parse_fh(\*data); $root = $doc->documentelement(); ($node) = $root->findnodes('//node[@id="7"]'); $text = following_text($node); $text;
__data__ <root> <node id="7"/> www <node id="10"/> bar </root> * — www should child of node. example, <node id="7">www</node> better.
Comments
Post a Comment