I'm trying to figure out XML::LibXML. It's rough going. I need to be able to access the text content of a node by name. Here is my code:
#!/usr/bin/perl
use strict;
use XML::LibXML;
my $string = qq~<?xml version="1.0"?>
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceS
+ervice/2013-08-01">
<Items>
<Item>
<ASIN>B01KI4JSQY</ASIN>
</Item>
</Items>
</ItemLookupResponse>
~;
my $parser = XML::LibXML->new->load_xml(string => $string, {no_blanks
+=> 1});
my $xml = XML::LibXML::XPathContext->new($parser);
$xml->registerNs('x', 'http://webservices.amazon.com/AWSECommerceServi
+ce/2013-08-01');
# Parse items
foreach my $item ($xml->findnodes('/x:ItemLookupResponse/x:Items/x:Ite
+m', $parser)){
print $item->firstChild->nodeName, "\n";
print $item->firstChild->toString, "\n";
print $item->findvalue('ASIN'), "\n";
print $item->findvalue('./ASIN'), "\n";
print $item->findvalue('./ASIN', $item), "\n";
}
Expected result:
ASIN
<ASIN>B01KI4JSQY</ASIN>
B01KI4JSQY
B01KI4JSQY
B01KI4JSQY
Actual result:
ASIN
<ASIN>B01KI4JSQY</ASIN>
Probably I'm just misunderstanding something in the docs. But I don't know what it is. I tried $item->findvalue('x:ASIN'), but that threw an error. Please help. I have a family. If I become an alcoholic, they will suffer.