G'day calebcall,

Using Data::Dump to show the structure of $result:

#!/usr/bin/env perl use strict; use warnings; use XML::Simple; my $xml = XML::Simple->new(); my $result = $xml->XMLin(<<'EOX'); <?xml version="1.0" encoding="utf-8"?> <ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response" +> <Errors /> <Warnings /> <RequestedCommand>namecheap.domains.getList</RequestedCommand> <CommandResponse Type="namecheap.domains.getList"> <DomainGetListResult> <Domain ID="8888888" Name="Domain1.com" Expires="03/31/2015"/> <Domain ID="8888889" Name="Domain2.com" Expires="02/25/2015"/> <Domain ID="8888899" Name="Domain3.com" Expires="04/01/2015"/> <Domain ID="8888999" Name="Domain4.com" Expires="05/20/2015"/> </DomainGetListResult> <Paging> <TotalItems>4</TotalItems> <CurrentPage>1</CurrentPage> <PageSize>50</PageSize> </Paging> </CommandResponse> <Server>API02</Server> <GMTTimeDifference>--5:00</GMTTimeDifference> <ExecutionTime>0.008</ExecutionTime> </ApiResponse> EOX use Data::Dump; dd $result;

I get:

{ CommandResponse => { DomainGetListResult => { Domain => [ { Expires => "03/31/2015", ID => 8888888, Name => "Domain1.com +" }, { Expires => "02/25/2015", ID => 8888889, Name => "Domain2.com +" }, { Expires => "04/01/2015", ID => 8888899, Name => "Domain3.com +" }, { Expires => "05/20/2015", ID => 8888999, Name => "Domain4.com +" }, ], }, Paging => { CurrentPage => 1, PageSize => 50, TotalItems => 4 }, Type => "namecheap.domains.getList", }, Errors => {}, ExecutionTime => 0.008, GMTTimeDifference => "--5:00", RequestedCommand => "namecheap.domains.getList", Server => "API02", Status => "OK", Warnings => {}, xmlns => "http://api.namecheap.com/xml.response", }

So, changing your loop to this:

foreach my $domain (@{$result->{CommandResponse}{DomainGetListResult}{ +Domain}}) { print $domain->{Name} . "\n"; }

I get:

Domain1.com Domain2.com Domain3.com Domain4.com

-- Ken


In reply to Re: Parsing XML by kcott
in thread Parsing XML by calebcall

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.