Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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); }

In reply to Re: XML::LibXML drives me to drinking by ikegami
in thread XML::LibXML drives me to drinking by tunafish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-16 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found