tunafish has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::LibXML drives me to drinking
by Your Mother (Archbishop) on Oct 23, 2016 at 00:44 UTC | |
by tunafish (Beadle) on Oct 23, 2016 at 03:15 UTC | |
by Your Mother (Archbishop) on Oct 23, 2016 at 03:40 UTC | |
by tunafish (Beadle) on Oct 23, 2016 at 04:40 UTC | |
|
Re: XML::LibXML drives me to drinking
by Anonymous Monk on Oct 23, 2016 at 01:47 UTC | |
by tunafish (Beadle) on Oct 23, 2016 at 03:59 UTC | |
by Anonymous Monk on Oct 23, 2016 at 05:00 UTC | |
|
Re: XML::LibXML drives me to drinking
by ikegami (Patriarch) on Oct 24, 2016 at 18:15 UTC | |
|
Re: XML::LibXML drives me to drinking
by Jenda (Abbot) on Oct 25, 2016 at 14:28 UTC | |
|
Re: XML::LibXML drives me to drinking
by grantm (Parson) on Nov 11, 2016 at 01:43 UTC |