SPUG: XML::Simple

Adam Monsen meonkeys at hotmail.com
Sun Dec 9 20:13:53 CST 2001


Ryan,

If you still want help with this, could you post the XML and the code you're 
using to parse it? It may be easier to use XML::XPath to get right at the 
data you want.

Given this XML:
--START xml ---------------------------------------
<DataTribe>
<SendFunc>
   <AcknowledgeAddrs>
     <EmailAddress>amonsen at example.com</EmailAddress>
     <EmailAddress>meonkeys at hotmail.com</EmailAddress>
   </AcknowledgeAddrs>
   <ClientName>MarketSuckemdry &amp; Buy, INC.</ClientName>
   <ImportData>
     <Row>
       <UserID>0157H7</UserID>
       <Column Name="Car">Honda</Column>
       <Column Name="Sex">Female</Column>
     </Row>
   </ImportData>
</SendFunc>
</DataTribe>
--END   xml ---------------------------------------

To extract the content within the ClientName element, you'd do something 
like this:
--BEGIN XML-XPath_test.pl--------------------------
#!/usr/bin/perl -w

use XML::XPath;
use XML::XPath::XMLParser;
use strict;

my $xml;
while (<>) { $xml .= $_; }

my $parser = XML::XPath::XMLParser->new(xml => $xml);
my $root_node = $parser->parse;
my $xpath = XML::XPath->new(context => $root_node);
my $clientname = $xpath->find('/DataTribe/SendFunc/ClientName/text()');
print "Client is $clientname for this transaction\n";
--END   XML-XPath_test.pl--------------------------


>From: "Ryan Ames" <ryan at e-valuations.com>
>To: <spug-list at pm.org>
>Subject: SPUG: XML::Simple
>Date: Thu, 6 Dec 2001 18:27:19 -0800
>
>Hello,
>	I am parsing a xml file using xml::simple and getting the
>following data structure when using Data::Dumper.
>This is my call to XMLin():
>
>my $xsl = XML::Simple->new(forcearray => ['page','question','answer']);
>
>forcearray is supposed to force the items listed into array context.  It
>is for 'page' and 'answer' but not for 'question'.
>Any reason for it not doing 'question'?
>Also, I am needing to grab the values of the 'answer_id' but I can't
>seem to figure out how to reference them.  Any help
>would be appreciated.  Thanks.
>
>
>
>$VAR1 = {
>
>           'pages' => {
>                        'page' => [
>                                    {
>
>                                      'questions' => {
>                                                       'question' => {
>
>'HASH(0x837d908)' => {
>
>
>
>'answers' => {
>
>'answer' => [
>
>{
>
>
>
>'answer_id' => 257993
>
>},
>
>{
>
>
>
>'answer_id' => 257994
>
>},
>
>{
>
>
>
>'answer_id' => 257995
>
>},
>
>{
>
>
>
>'answer_id' => 257996
>
>},
>
>{
>
>
>
>'answer_id' => 257997
>
>}
>
>]
>
>},
>
>
>
>'ordering_question_id' => {}
>
>}
>                                                                     }
>                                                     },
>
>                                      'points' => {}
>                                    }
>                                  ]
>                      },
>           'session_id' => 810,
>
>         };
>
>
>
>Ryan
>
>
>
>
>
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
>       Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
>   Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
>  For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
>      Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/
>
>


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list