TravelAddict has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I'm trying to extract elements from nodes in XML using XML::LibXML, and I need to extract the elements that could be embedded within that node.
For example, I have this XML string: <doc><text>From mobile, <ph1 i="1" type="33" x="1"/>dial<ph2 i="1"/> this number:</text></doc>
What I need as a result is this: From mobile, <ph1 i="1" type="33" x="1"/>dial<ph2 i="1"/> this number:, but I cannot extract the elements embedded (<ph1 i="1" type="33" x="1"/> and <ph2 i="1"/>), they disappear no matter what I try.
This is the sample code to reproduce the issue:
use strict; use warnings; use XML::LibXML; my $xmlString = "<doc><text>From mobile, <ph1 i=\"1\" type=\"33\" x=\" +1\"/>dial<ph2 i=\"1\"/> this number:</text></doc>"; my $dom = XML::LibXML->load_xml(string => $xmlString); my $to_literal = $dom->to_literal('/doc/text'); print "to_literal:\t$to_literal\n"; my $findvalue = $dom->findvalue('/doc/text'); print "findvalue:\t$findvalue\n"; my $textContent = $dom->textContent('/doc/text'); print "textContent:\t$textContent\n";
This is what I get when I run this code:
to_literal: From mobile, dial this number: findvalue: From mobile, dial this number: textContent: From mobile, dial this number:
It looks like "to_literal", "findvalue" and "textContent" take out the embedded placeholders, but I want to keep them in.
Is there any way to get what I need with a simple method?
Thanks very much in advance!
TA
|
|---|