2

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.

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
Ras
  • 53
  • 6
  • Have you installed `XML::LibXML`? You can do this by `cpan install XML::LibXML` – Sobrique Oct 14 '16 at 07:57
  • Also: turn on `use strict;` and `use warnings;`. Also: Fix your shebang path. You probably need `#!/usr/bin/perl`. – Sobrique Oct 14 '16 at 07:59
  • Did you install LibXML with `sudo`? Otherwise, your include paths should also contain your user's perl include path. – Martin Nyolt Oct 14 '16 at 08:10
  • You might benefit from my tutorial: [Perl XML::LibXML by Example](http://grantm.github.io/perl-libxml-by-example/). In this case the main problem seems to be that you have `use XML::LibXML::Element;` rather than `use XML::LibXML;`. – Grant McLean Nov 11 '16 at 01:48

1 Answers1

4

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
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133