Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
| Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2009, 12:47 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Question XML::Simple is this possible

Hi,

I'm a beginner in perl.
I'm trying to get an element value in xml.
I'm using XML::Simple.

BODY{font:x-small 'Verdana';margin-right:1.5em} .c{cursor:hand} .b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none} .e{margin-left:1em;text-indent:-1em;margin-right:1em} .k{margin-left:1em;text-indent:-1em;margin-right:1em} .t{color:#990000} .xt{color:#990099} .ns{color:red} .dt{color:green} .m{color:blue} .tx{font-weight:bold} .db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier} .di{font:small Courier} .d{color:blue} .pi{color:blue} .cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888} .ci{font:small Courier;color:#888888} PRE{margin:0px;display:inline}
Code:
<?xml version="1.0" ?> 
  <resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <row>
     <field name="Customer_ID">1</field> 

    <field name="Name">ABC</field> 

   </row>


  <row>
     <field name="Customer_ID">2</field> 

    <field name="Name">DEF</field> 

   </row>
</resultset>
I'm trying to get the customer_ID value.
Code:
my $myXML = XMLin("/myxml.xml");
my $custid = $myXML->{row}->{field=>['Name'=>'DEF']}->{field=>['Customer_ID']}
Can someone tell me what did I do wrong?
 
Old September 3rd, 2009, 04:04 AM
Registered User
 
Join Date: Aug 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you be a little clearer on just what you want?

Data::Dumper can help you here.
Your data structure looks like this:
Code:
$VAR1 = {
          'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
          'row' => [
                   {
                     'field' => {
                                'Name' => {
                                          'content' => 'ABC'
                                        },
                                'Customer_ID' => {
                                                 'content' => '1'
                                               }
                              }
                   },
                   {
                     'field' => {
                                'Name' => {
                                          'content' => 'DEF'
                                        },
                                'Customer_ID' => {
                                                 'content' => '2'
                                               }
                              }
                   }
                 ]
        };
This may be what you actually want:
Code:
use strict;
use warnings;
use XML::Simple;
my $myXML = XMLin("./myxml.xml");
print "\n";
foreach my $custid(@{$myXML->{row}}) {
	print "Customer ID: ".$custid->{'field'}->{'Customer_ID'}->{'content'}."\n";
}

Last edited by Leigh; September 3rd, 2009 at 04:08 AM..




Similar Threads
Thread Thread Starter Forum Replies Last Post
XML::Simple parsing xml geek.shrek Perl 1 August 19th, 2009 10:32 PM
Need Xsl to Sort the following simple xml: Takashi321 XSLT 3 August 27th, 2007 11:54 AM
ch02 - Binding to a Simple XML Document Tophat BOOK: Beginning ASP.NET 1.0 7 August 17th, 2007 05:09 AM
simple xml / xslt example needed badgolfer ASP.NET 1.0 and 1.1 Basics 2 January 21st, 2005 02:10 AM
Getting Simple XML Reading In ASP Happening munkimagik XML 1 June 19th, 2003 09:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.