Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: XML::LibXML drives me to drinking

by ikegami (Patriarch)
on Oct 24, 2016 at 18:15 UTC ( [id://1174618]=note: print w/replies, xml ) Need Help??


in reply to XML::LibXML drives me to drinking

You used the proper namespace here:

$xml->findnodes('/x:ItemLookupResponse/x:Items/x:Item')

But you forgot here:

$item->findvalue('ASIN')

Change the latter to

$xml->findvalue('x:ASIN', $item)

Other issues:

  • Your variable names are awful! You call your XML $string while $xml is an XPathContext object for which $xpc is recommended, and you call your document $parser where $doc or $dom make more sense.

  • You call load_xml as an object method, but it's a static method like new.

  • XML::LibXML::XPathContext isn't loaded by use XML::LibXML; in older versions of XML::LibXML.


Fixed:

#!/usr/bin/perl use strict; use warnings qw( all ); use feature qw( say ); use XML::LibXML qw( ); use XML::LibXML::XPathContext qw( ); my $xml = <<'__EOS__'; <?xml version="1.0"?> <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceS +ervice/2013-08-01"> <Items> <Item> <ASIN>B01KI4JSQY</ASIN> </Item> </Items> </ItemLookupResponse> __EOS__ my $doc = XML::LibXML->load_xml(string => $xml, { no_blanks => 1 }); my $xpc = XML::LibXML::XPathContext->new(); $xpc->registerNs('x', 'http://webservices.amazon.com/AWSECommerceServi +ce/2013-08-01'); for my $item ($xpc->findnodes('/x:ItemLookupResponse/x:Items/x:Item', +$xml)) { say $item->firstChild->nodeName; say $item->firstChild->toString; say $xpc->findvalue('x:ASIN', $item); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174618]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-03-19 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found