CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
XML vale fetching

 



ym_chaitu
Novice

May 19, 2010, 2:10 AM

Post #1 of 8 (1259 views)
XML vale fetching Can't Post

hai i require one more help
i have an xml like this


Code
<?xml version="1.0"?> 
<spam-document version="3.5" timestamp="2002-05-13 15:33:45">
<!-- Autogenerated by WarbleSoft Spam Version 3.5 -->
<customer>
<first-name>Joe</first-name>
<surname>Wrigley</surname>
<address>
<street>17 Beable Ave.</street>
<city>Meatball</city>
<state>MI</state>
<zip>82649</zip>
</address>
<email>joewrigley@jmac.org</email>
<age>42</age>
</customer>
<customer>
<first-name>Henrietta</first-name>
<surname>Pussycat</surname>
<address>
<street>R.F.D. 2</street>
<city>Flangerville</city>
<state>NY</state>
<zip>83642</zip>
</address>
<email>meow@263A.org</email>
<age>37</age>
</customer>
</spam-document>


now what i wanted is that i need to fetch the values of tag email and show them.
how do i do it..

Code
use XML::Simple; 
my $conf=XMLin('x.xml',forcearray=>1);
use Data::Dumper;
print Dumper($conf);
print $conf->{timestamp}."\n";
@email=$conf->{customer}->{email};
foreach(@email)
{
print "$_\n";
}


this code which i worte is not giving any output..
can some one help me..


7stud
Enthusiast

May 19, 2010, 3:38 AM

Post #2 of 8 (1257 views)
Re: [ym_chaitu] XML vale fetching [In reply to] Can't Post

If you look at the output from Data::Dumper, the email address of each customer is stored in an array. That is what forcearray=>1 does. So, this line

$conf->{customer}->{email}

returns a reference to an array, and the array contains one email address. So the email address is:

$conf->{customer}->{email}->[0]



Code
use strict; 
use warnings;
use 5.010;

use XML::Simple qw{ :strict };
my $doc_href = XMLin('xml4.xml', forcearray=>1, KeyAttr=>[]);

use Data::Dumper;
say Dumper($doc_href);

my $customer_aref = $doc_href->{customer};

for my $single_customer_href (@$customer_aref) {
my $email = $single_customer_href->{email}->[0];
say $email;
}

--output:--
$VAR1 = {
'timestamp' => '2002-05-13 15:33:45',
'customer' => [
{
'email' => [
'joewrigley@jmac.org'
],
'first-name' => [
'Joe'
],
'address' => [
{
'zip' => [
'82649'
],
'city' => [
'Meatball'
],
'street' => [
'17 Beable Ave.'
],
'state' => [
'MI'
]
}
],
'age' => [
'42'
],
'surname' => [
'Wrigley'
]
},
{
'email' => [
'meow@263A.org'
],
'first-name' => [
'Henrietta'
],
'address' => [
{
'zip' => [
'83642'
],
'city' => [
'Flangerville'
],
'street' => [
'R.F.D. 2'
],
'state' => [
'NY'
]
}
],
'age' => [
'37'
],
'surname' => [
'Pussycat'
]
}
],
'version' => '3.5'
};

joewrigley@jmac.org
meow@263A.org



(This post was edited by 7stud on May 19, 2010, 3:52 AM)


ym_chaitu
Novice

May 19, 2010, 3:58 AM

Post #3 of 8 (1246 views)
Re: [7stud] XML vale fetching [In reply to] Can't Post

Thank u 7stud
it worked


ym_chaitu
Novice

May 19, 2010, 4:02 AM

Post #4 of 8 (1245 views)
Re: [ym_chaitu] XML vale fetching [In reply to] Can't Post


In Reply To
Thank u 7stud
it worked


but the thing is that if i give the same for the first-name it is giving me an error..

Mad
this is the code i used..

Quote
my $fname = $customer_href->{first-name}->[0];



7stud
Enthusiast

May 19, 2010, 5:27 AM

Post #5 of 8 (1243 views)
Re: [ym_chaitu] XML vale fetching [In reply to] Can't Post

Normally, hash keys are strings, which you need to put quotes around. But perl can work out single names when you don't use quotes, for example 'customer' and 'email'. But to perl, the fragment:

first-name

looks like a variable called 'first' minus a variable called 'name'.


(This post was edited by 7stud on May 19, 2010, 5:28 AM)


ym_chaitu
Novice

May 19, 2010, 5:44 AM

Post #6 of 8 (1240 views)
Re: XML vale fetching [In reply to] Can't Post

thank u now that is working


ym_chaitu
Novice

May 20, 2010, 7:14 AM

Post #7 of 8 (1234 views)
Re: [7stud] XML vale fetching [In reply to] Can't Post

hai again got stuck into a problem
now the thing is that i require to get the values from the address->zip
i have tried ur formula
but is not working..Crazy


Code
use strict;  
use warnings;
#use 5.010;

use XML::Simple qw{ :strict };

my $doc_href = XMLin('x.xml', forcearray=>1, KeyAttr=>[]);

use Data::Dumper;

print Dumper($doc_href);

my $customer_aref = $doc_href->{customer};
my $customer_aref_1 = $doc_href->{customer}->{address}->[0];
for my $customer_href (@$customer_aref) {
my $email = $customer_href->{email}->[0];
my $fname = $customer_href->{'first-name'}->[0];

print $email."\n";
print $fname."\n";


}
for my $custi (@$customer_aref_1)
{
my $addre_zip = $custi->{'zip'}->[0];
print $addre_zip;
}


i have tried it like this
as well as like this too


Code
use strict;  
use warnings;
#use 5.010;

use XML::Simple qw{ :strict };

my $doc_href = XMLin('x.xml', forcearray=>1, KeyAttr=>[]);

use Data::Dumper;

print Dumper($doc_href);

my $customer_aref = $doc_href->{customer};

for my $customer_href (@$customer_aref) {
my $email = $customer_href->{email}->[0];
my $fname = $customer_href->{'first-name'}->[0];
my $addre_zip = $customer_href->{'address'}->{'zip'}->[0];
print $email."\n";
print $fname."\n";
print $addre_zip;

}

but both are not working

can u help me


ym_chaitu
Novice

Jun 14, 2010, 11:19 PM

Post #8 of 8 (1198 views)
Re: [ym_chaitu] XML vale fetching [In reply to] Can't Post

anyone got any ideas or solutions for this??

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives