Search code examples
xmlperlxml-libxml

Perl: Get value from XML


I need to get value from this xml - http://pastebin.com/wkh7trd4. Here is my code, but it return nothing:

my $xml = XML::LibXML->new('clean_namespaces' => 1)->parse_file("$tmp_dir/xml/$xml_file_name");
my $xc = XML::LibXML::XPathContext->new($xml);
my $val = $xc->findvalue('/ns2:export/ns2:bankGuarantee/docPublishDate');
print $val;

Solution

  • Looks like it has to do with default name space. Not sure how xpath works but @miller seemed to answer his own question here: XML::LibXML, namespaces and findvalue

    You can try the below which should hopefully resolve your issue

    use strict;
    use warnings;
    use XML::LibXML;
    
    open(my $xml_file, '<', "xml_to_parse.xml");
    my $xml = new XML::LibXML->load_xml(IO => $xml_file);
    print $xml->findvalue('/ns2:export/ns2:bankGuarantee/*[local-name()="docPublishDate"]'), "\n";