I have installed XML:LibXML
using CPAN but still getting the below error:
Can't locate XML/LibXML/Element.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .)
Below is my code:
#!usr/bin/perl
use XML::LibXML::Element;
my $pxml = '/cctest/projects.xml';
my $twigp = XML::LibXML->new-> parse_file($pxml);
my $result = $twigp->getChildrenByTagName('branched_from_id');
print $result->to_literal,"\n";
projects.xml:
<projects>
<project>
<id>ID_2_19_16_12_15</id>
<name>RPSW </name>
<branch>fb16</branch>
<location>/draw/projects</location>
<author>Ras</author>
<branched_from_id>ID_10_8_13_12_35</branched_from_id>
<branched_from_version>175</branched_from_version>
</project>
<project>
<id>ID_1_21_14_1_13_24_PM</id>
<name>Platform</name>
<location>/draw/projects</location>
<author>lav</author>
<assigned_user>ka</assigned_user>
</project>
</projects>
Please help me to identify where am I doing wrong? Requirement is to get only the nodes which are having the element branched_from_id
.
I get the same Can't locate module
error if I use your code. But it goes away if I use
use XML::LibXML;
$node = XML::LibXML::Element->new( $name );
Same is mentioned under the synopsis of the module.
You get error because there is no Element.pm file. XML::LibXML::Element is a package inside LibXML.pm. See: https://metacpan.org/source/SHLOMIF/XML-LibXML-2.0128/LibXML.pm
For example if you have a module Mod::Test
as
package Mod::Test;
#do something
package Mod::Test::Another;
#do something
1;
and you use it in a script as
#!/usr/bin/perl
use strict;
use warnings;
use Mod::Test::Another;
Then you will get the error, but if you use Mod::Test
then you'll not get the error.
BTW below is an approach to find tag without using XML::LibXML::Element.
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $filename = "projects.xml";
my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filename);
for my $node ($xmldoc->findnodes('/projects/project')) {
for my $property ($node->findnodes('./*')) {
if ($property->nodeName() eq 'branched_from_id'){
print "Found branched_from_id node, value is ".$property->textContent();
}
}
print "\n";
}
Output:
$ perl test.pl
Found branched_from_id node, value is ID_10_8_13_12_35