How to get child nodes in XML :: LibXML of type node?
I am parsing complex XML documents and one section might look like this:
<mds>
<md>
<value>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><record>...</record>]]>
</value>
</md>
</mds>
When I parse the node value, it actually contains 3 child nodes, two empty and one cdata node. Is there a way to easily get the cdata-node like
my @dcvalues = $dom->findnodes("//mds/md/value");
my @cdatanodes = $dcvalues[0]->find(<some xpath that only returns cdata nodes>);
my $cdataval = $cdatanodes[0]->textContent;
You get the idea. Edit: I know that I could just access the cdata in this example with
my $cdatanode = $dcvalues[0]->firstChild->nextSibling;
but then I will depend on cdata to always be the second node, which I am not sure about.
+3
source to share
1 answer
You need a parameter no_blanks
. Like this
use strict;
use warnings;
use 5.010;
use XML::LibXML;
my $xml = XML::LibXML->load_xml(string => <<END_XML, {no_blanks => 1});
<mds>
<md>
<value>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><record>...</record>]]>
</value>
</md>
</mds>
END_XML
my @values = $xml->findnodes('//mds/md/value/text()');
say scalar @values;
say say $values[0]->textContent;
Output
1
<?xml version="1.0" encoding="UTF-8"?><record>...</record>
+6
source to share