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.
-
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.