Search code examples
perlxpathxml-libxml

Print output using XML::LibXML


my $doc = $parser->parse_string( $res->content );
my $root = $doc->getDocumentElement;
my @objects = $root->getElementsByTagName('OBJECT');

foreach my $object ( @objects ){
my $name = $object->firstChild;
print "OBJECT = " . $name . "\n";}


OUTPUT is:
OBJECT = XML::LibXML::Text=SCALAR(0x262e170)
OBJECT = XML::LibXML::Text=SCALAR(0x2ee4b00)
OBJECT = XML::LibXML::Text=SCALAR(0x262e170)
OBJECT = XML::LibXML::Text=SCALAR(0x2ee4b00)

Can anyone please explain why print prints the $name attribute values like this? Why does it print normal when I use the function getAttribute with virtually he same code?


Solution

  • getAttribute returns an attribute, while firstChild returns a text node, element, processing instruction, or a comment.

    What you see is a normal Perl way of printing an object: it prints its class and address. Your version of XML::LibXML seems to be a bit antique, recent versions overload the stringification and the code produces the actual text node.

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    use XML::LibXML;
    
    my $doc = 'XML::LibXML'->load_xml( string => << '__XML__');
    <root>
        <OBJECT name="o1">hello</OBJECT>
    </root>
    __XML__
    
    my @objects = $doc->getElementsByTagName('OBJECT');
    
    for my $object (@objects) {
        print 'OBJECT = ', $object->firstChild, "\n";
    }
    

    Output:

    OBJECT = hello
    

    In the old versions, one needed to call the nodeValue or data method.

    print 'OBJECT = ', $object->firstChild->data, "\n";